library(tidyverse)
library(ggforce)
library(stringr)
library(cjoint)
library(cregg) # Install with remotes::install_github("leeper/cregg")
library(fixest)
library(broom)
library(cowplot)
library(modelsummary)

# Set the directory to the location of the replication data
setwd("")

source("ggplot_theme.R")

# Was saved as rds rather than csv to keep factor level orderings
C <- read_rds("Data/Paired_Conjoint_Data.rds")


########## PAIRED CONJOINT EXPERIMENT

# Many regressions are separated by citizen respondents and politicians
# respondents. This function runs the same regression model on both subsets
# and outputs results in a stacked data.frame for both
paired_conjoint <- function(data1, data2, label1, label2, group = "None",
                            formula = chose_profile ~ text_pol +
                                                      text_user +
                                                      party_pol_copartisan +
                                                      woman_user +
                                                      woman_pol +
                                                      gendered) {

    amce_model <- list()
    for(i in 1:2) {

        if(i == 1) data <- data1
        if(i == 2) data <- data2
        
        n <- sum(!is.na(data$chose_profile))
        n_clusters <- length(na.omit(unique(data$id[!is.na(data$chose_profile)])))

        model90 <- cregg::amce(formula, id = ~ id, alpha = 0.1,
                                        data = subset(data, !is.na(chose_profile)))
        amce_model[[i]] <- cregg::amce(formula, id = ~ id,
                                                data = subset(data, !is.na(chose_profile)))
        amce_model[[i]]$lower90 <- model90$lower
        amce_model[[i]]$upper90 <- model90$upper
        amce_model[[i]]$n <- n
        amce_model[[i]]$n_clusters <- n_clusters

    }

    E <- bind_rows(tibble(amce_model[[1]], respondent_type = label1, group = group),
                   tibble(amce_model[[2]], respondent_type = label2, group = group))

    E <- E %>%
         mutate(feature = recode(feature, "gendered" = "Gendered text",
                                          "text_pol_group" = "Content of politician's post",
                                          "text_user" = "Reply from user",
                                          "woman_pol" = "Gender of politician",
                                          "woman_user" = "Gender of user",
                                          "resp_gender" = "Gender of respondent",
                                          "resp_left_wing" = "Left-wing respondent",
                                          "party_pol_copartisan" = "Co-partisan politician",
                                          "poc_pol" = "Politician is a Person of Color",
                                          "woman_pol_gendered" = "Interaction",
                                          "woman_pol_man_user" = "Interaction",
                                          "woman_pol_resp_woman" = "Interaction",
                                          "woman_pol_resp_left_wing" = "Interaction",
                                          "man_user_resp_left_wing" = "Interaction",
                                          "man_user_resp_woman" = "Interaction",
                                          "man_user_gendered" = "Interaction",
                                          "gendered_resp_woman" = "Interaction",
                                          "woman_pol_text_pol_group" = "Interaction",
                                          "woman_pol_man_user_resp_woman" = "Triple interaction",
                                          "woman_pol_man_user_resp_left_wing" = "Triple interaction",
                                          "woman_pol_man_user_gendered" = "Triple interaction",
                                          "woman_pol_gendered_resp_woman" = "Triple interaction"),
                level = recode(level, "Woman politician & Gendered text" = "Woman politician\nx gendered text",
                                      "Woman politician & Man user" = "Woman politician\nx man user",
                                      "Woman politician & Female" = "Woman politician\nx man user",
                                      "Man user & Female" = "Man user\nx woman respondent",
                                      "Woman politician & Left-wing" = "Woman politician\nx left-wing respondent",
                                      "Man user & Left-wing" = "Man user\nx left-wing respondent",
                                      "Gendered text & Female" = "Gendered text\nx woman respondent",
                                      "Woman politician & Man user & Left-wing" = "Woman politician\nx man user\nx left-wing",
                                      "Woman politician & Man user & Female" = "Woman politician\nx man user\nx woman respondent",
                                      "Woman politician & Man user & Gendered text" = "Woman politician\nx man user\nx gendered text",
                                      "Woman politician & Gendered text & Female" = "Woman politician\nx gendered text\nx woman respondent")) %>%
          mutate(feature = factor(feature, levels = c("Triple interaction",
                                                      "Interaction",
                                                      "Gender of politician",
                                                      "Gendered text",
                                                      "Gender of user",
                                                      "Gender of respondent",
                                                      "Left-wing respondent",
                                                      "Co-partisan politician",
                                                      "Politician is a Person of Color",
                                                      "Content of politician's post",
                                                      "Reply from user")))

    E <- filter(E, !(is.na(std.error) & feature %in% c("Interaction", "Triple interaction")))

    names(E)[names(E) == "p"] <- "p.value"

    return(E)

}

get_regression_table <- function(df) {

    ms_object <- data.frame(term = as.character(df$level[!is.na(df$std.error)]),
                                      estimate = df$estimate[!is.na(df$std.error)],
                                      std.error = df$std.error[!is.na(df$std.error)],
                                      p.value = df$p.value[!is.na(df$std.error)])
    ms_object$term <- str_replace_all(ms_object$term, "PolText", "Politician text ")
    ms_object$term <- str_replace_all(ms_object$term, "UserText", "User text ")
    ms_object$term <- str_replace_all(ms_object$term, " 0", " ")
    
    gl <- data.frame(`N observations` = scales::comma(unique(df$n)),
                     `N respondents` = scales::comma(unique(df$n_clusters)))

    ms_object <- list(tidy = ms_object,
                      glance = gl)
    class(ms_object) <- "modelsummary_list"

    return(ms_object)

}


# FIGURE 3 BUT WITH POOLING POLITICIANS AND CITIZENS TO TALK ABOUT EFFECT
# SIZES GENERALLY
G1 <- paired_conjoint(filter(C, respondent_type == "Citizen" & !is.na(chose_profile)),
                      filter(C, respondent_type == "Politician" & !is.na(chose_profile)),
                      label1 = "Citizen sample", label2 = "Politician sample")

# In Figure 3 figure note
cat(paste0("\n\nCitizen sample observations~$=$~",
           scales::comma(unique(G1$n[G1$respondent_type == "Citizen sample"])),
           " (",
           scales::comma(unique(G1$n_clusters[G1$respondent_type == "Citizen sample"])),
           " respondents); politician sample observations~$=$~",
           scales::comma(unique(G1$n[G1$respondent_type == "Politician sample"])),
           " (",
           scales::comma(unique(G1$n_clusters[G1$respondent_type == "Politician sample"])),
           " respondents).\n\n"))

# Average effect size of a woman politician for citizen and politician
# respondents ~ 6 %-points (Hypothesis 1)
mean(G1$estimate[G1$level == "Woman politician"])
# p-vales both well below 0.001
G1[G1$level == "Woman politician", ]

# Interaction between citizen and politician respondents for the gender of the
# politician
C$respondent_type <- factor(C$respondent_type, levels = c("Citizen", "Politician"))
model_hyp2 <- cregg::amce(chose_profile ~ text_pol + text_user + party_pol_copartisan +
                            woman_user + woman_pol + respondent_type +
                            woman_pol_respondent_type +
                            gendered,
            id = ~ id, alpha = 0.05,
            data = subset(C, !is.na(chose_profile)))
# p < 0.05 (Hypothesis 2)
model_hyp2[model_hyp2$level == "Woman politician & Politician", ]


# Gendered message
mean(G1$estimate[G1$level == "Gendered text"]) # ~ 7 % points, p < 0.001
# p-vales both well below 0.001
G1[G1$level == "Gendered text", ] %>% select("respondent_type", "estimate", "p.value")

# Gender of user sending the message
# positive, but only significant for the citizen sample
G1[G1$level == "Man user", ] %>% select("respondent_type", "estimate", "p.value")

# Co-partisan of the politician
# Not significant for citizens or politicians, p = 0.24, p = 0.90
G1[G1$level == "Co-partisan", ] %>% select("respondent_type", "estimate", "p.value")




# FIGURE 3
G1_country <- bind_rows(
              tibble(paired_conjoint(filter(C, respondent_type == "Citizen" & country == "Belgium"),
                                     filter(C, respondent_type == "Politician" & country == "Belgium"),
                                     label1 = "Citizen sample", label2 = "Politician sample"),
                     country = "Belgium"),
              tibble(paired_conjoint(filter(C, respondent_type == "Citizen" & country == "Chile"),
                                     filter(C, respondent_type == "Politician" & country == "Chile"),
                                     label1 = "Citizen sample", label2 = "Politician sample"),
                     country = "Chile"),
              tibble(paired_conjoint(filter(C, respondent_type == "Citizen" & country == "Denmark"),
                                     filter(C, respondent_type == "Politician" & country == "Denmark"),
                                     label1 = "Citizen sample", label2 = "Politician sample"),
                     country = "Denmark"),
              tibble(paired_conjoint(filter(C, respondent_type == "Citizen" & country == "United States"),
                                     filter(C, respondent_type == "Politician" & country == "United States"),
                                     label1 = "Citizen sample", label2 = "Politician sample"),
                     country = "US"))

G1_country <- bind_rows(G1_country, tibble(G1, country = "Pooled estimate"))
G1_country$country <- factor(G1_country$country, levels = c("Belgium", "Chile", "Pooled estimate", "Denmark", "US"))

G1_country_citizen <- ggplot(filter(G1_country, feature != "Reply from user"),
       aes(x = estimate, xend = estimate, y = level, yend = level,
           color = country, size = country,
           shape = country, fill = country,
           alpha = country)) +
     my.theme(base_size = 7, borderless = 2, remove.ticks.y = TRUE, remove.ticks.x = TRUE,
              grid.y_colour = "grey65", grid.y_linetype = 3,
              grid.x_colour = "grey85", grid.x_linetype = 1) +
     labs(x = "AMCE (in percentage points)", y = "") +
     ggforce::facet_col( ~ feature, scales = "free_y", space = "free") +
     coord_cartesian(xlim = c(-0.05, 0.12)) +
     scale_x_continuous(breaks = seq(-0.06, 0.12, by = 0.02),
                        labels = c("-6", "-4", "-2", "0", "2", "4", "6", "8", "10", "12")) +
     geom_vline(xintercept = 0, linetype = 1, size = 0.35, color = "grey50") +
     geom_point(size = 0, color = "transparent") +
     geom_linerange(data = filter(G1_country, feature != "Reply from user" & !is.na(std.error) & respondent_type == "Citizen sample"),
                    aes(xmin = lower, xmax = upper, alpha = country),
                    position = position_dodge2(width = 1),
                    size = 0.5, show.legend = FALSE) +
     geom_point(data = filter(G1_country, feature != "Reply from user" & !is.na(std.error) & respondent_type == "Citizen sample"),
                position = position_dodge2(width = 1), alpha = 1) +
     geom_point(data = filter(G1_country, country == "Pooled estimate", feature != "Reply from user" & !is.na(std.error) & respondent_type == "Citizen sample"),
                position = position_dodge2(width = 1), stroke = 0.8) +
     geom_point(data = filter(G1_country, country == "Pooled estimate", feature != "Reply from user" & is.na(std.error) & respondent_type == "Citizen sample"),
                size = 1.5, shape = 16, alpha = 1, color = "black", fill = "black") +
     scale_size_manual(values = c(0.75, 0.75, 2.5, 0.75, 0.75)) +
     scale_shape_manual(values = c(16, 16, 23, 16, 16)) +
     scale_fill_manual(values = c("#00bd8e", "#FF3E00", "white", "#A1B811", "#5200FF")) +
     scale_colour_manual(values = c("#00bd8e", "#FF3E00", "black", "#A1B811", "#5200FF")) +
     scale_alpha_manual(values = c(0.5, 0.5, 1, 0.5, 0.5)) +
     theme(legend.position = "top",
           strip.text.x = element_text(angle = 0, hjust = 0, size = 7, face = "bold")) +
     guides(size = guide_legend(override.aes = list(size = 2, alpha = 1)))

G1_country_politician <- ggplot(filter(G1_country, feature != "Reply from user"),
       aes(x = estimate, xend = estimate, y = level, yend = level,
           color = country, size = country,
           shape = country, fill = country,
           alpha = country)) +
     my.theme(base_size = 7, borderless = 2, remove.ticks.y = TRUE, remove.ticks.x = TRUE,
              grid.y_colour = "grey65", grid.y_linetype = 3,
              grid.x_colour = "grey85", grid.x_linetype = 1) +
     labs(x = "AMCE (in percentage points)", y = "") +
     ggforce::facet_col( ~ feature, scales = "free_y", space = "free") +
     coord_cartesian(xlim = c(-0.05, 0.12)) +
     scale_x_continuous(breaks = seq(-0.06, 0.12, by = 0.02),
                        labels = c("-6", "-4", "-2", "0", "2", "4", "6", "8", "10", "12")) +
     geom_vline(xintercept = 0, linetype = 1, size = 0.35, color = "grey50") +
     geom_point(size = 0, color = "transparent") +
     geom_linerange(data = filter(G1_country, feature != "Reply from user" & !is.na(std.error) & respondent_type == "Politician sample"),
                    aes(xmin = lower, xmax = upper),
                    position = position_dodge2(width = 1),
                    size = 0.5, show.legend = FALSE) +
     geom_point(data = filter(G1_country, feature != "Reply from user" & !is.na(std.error) & respondent_type == "Politician sample"),
                position = position_dodge2(width = 1), alpha = 1) +
     geom_point(data = filter(G1_country, country == "Pooled estimate", feature != "Reply from user" & !is.na(std.error) & respondent_type == "Politician sample"),
                position = position_dodge2(width = 1), stroke = 0.8) +
     geom_point(data = filter(G1_country, country == "Pooled estimate", feature != "Reply from user" & is.na(std.error) & respondent_type == "Politician sample"),
                size = 1.5, shape = 16, alpha = 1, color = "black", fill = "black") +
     scale_size_manual(values = c(0.75, 0.75, 2.5, 0.75, 0.75)) +
     scale_shape_manual(values = c(16, 16, 23, 16, 16)) +
     scale_fill_manual(values = c("#00bd8e", "#FF3E00", "white", "#A1B811", "#5200FF")) +
     scale_colour_manual(values = c("#00bd8e", "#FF3E00", "black", "#A1B811", "#5200FF")) +
     scale_alpha_manual(values = c(0.5, 0.5, 1, 0.5, 0.5)) +
     theme(legend.position = "top",
           strip.text.x = element_text(angle = 0, hjust = 0, size = 7, face = "bold")) +
     guides(size = guide_legend(override.aes = list(size = 2, alpha = 1)))

# FIGURE 3
# Note: PDF is edited in Illustrator after output to align labels & legend
pdf("Figures/Figure_3-RAW.pdf", 6.5, 4.5)
ggdraw() +
  draw_plot(G1_country_politician, x = -0.02, y = 0.01, width = 0.51, height = 1) +
  draw_plot(G1_country_citizen, x = 0.48, y = 0.01, width = 0.51, height = 1) +
  draw_plot_label(label = c("A.",
                            "B."),
                  x = c(0.01, 0.51), y = c(0.992, 0.992),
                  size = 8, hjust = 0, vjust = 1) +
  draw_plot_label(label = c("Politician sample",
                            "Citizen sample"),
                  x = c(0.035, 0.535), y = c(0.992, 0.992),
                  fontface = "plain",
                  size = 8, hjust = 0, vjust = 1)
dev.off()



# Complete regression results for Figure 3
ms_object_figure_3_Pol_US <- get_regression_table(filter(G1_country, respondent_type == "Politician sample" & country == "US"))
ms_object_figure_3_Cit_US <- get_regression_table(filter(G1_country, respondent_type == "Citizen sample" & country == "US"))
ms_object_figure_3_Pol_Denmark <- get_regression_table(filter(G1_country, respondent_type == "Politician sample" & country == "Denmark"))
ms_object_figure_3_Cit_Denmark <- get_regression_table(filter(G1_country, respondent_type == "Citizen sample" & country == "Denmark"))
ms_object_figure_3_Pol_Chile <- get_regression_table(filter(G1_country, respondent_type == "Politician sample" & country == "Chile"))
ms_object_figure_3_Cit_Chile <- get_regression_table(filter(G1_country, respondent_type == "Citizen sample" & country == "Chile"))
ms_object_figure_3_Pol_Belgium <- get_regression_table(filter(G1_country, respondent_type == "Politician sample" & country == "Belgium"))
ms_object_figure_3_Cit_Belgium <- get_regression_table(filter(G1_country, respondent_type == "Citizen sample" & country == "Belgium"))
ms_object_figure_3_Pol_Pooled <- get_regression_table(filter(G1_country, respondent_type == "Politician sample" & country == "Pooled estimate"))
ms_object_figure_3_Cit_Pooled <- get_regression_table(filter(G1_country, respondent_type == "Citizen sample" & country == "Pooled estimate"))


# What percentage of the 20 politician texts are statistically significant
# across the 4 country contexts
# As noted in Appendix M
# Among politician sample: 4% (3 of 76)
mean(c(ms_object_figure_3_Pol_US$tidy$p.value[str_detect(ms_object_figure_3_Pol_US$tidy$term, "^Politician text")],
       ms_object_figure_3_Pol_Denmark$tidy$p.value[str_detect(ms_object_figure_3_Pol_Denmark$tidy$term, "^Politician text")],
       ms_object_figure_3_Pol_Chile$tidy$p.value[str_detect(ms_object_figure_3_Pol_Chile$tidy$term, "^Politician text")],
       ms_object_figure_3_Pol_Belgium$tidy$p.value[str_detect(ms_object_figure_3_Pol_Belgium$tidy$term, "^Politician text")]) < 0.05)*100

# Among citizen sample: 4% (3 of 76) [6 of 152 in total in both samples]
mean(c(ms_object_figure_3_Cit_US$tidy$p.value[str_detect(ms_object_figure_3_Cit_US$tidy$term, "^Politician text")],
       ms_object_figure_3_Cit_Denmark$tidy$p.value[str_detect(ms_object_figure_3_Cit_Denmark$tidy$term, "^Politician text")],
       ms_object_figure_3_Cit_Chile$tidy$p.value[str_detect(ms_object_figure_3_Cit_Chile$tidy$term, "^Politician text")],
       ms_object_figure_3_Cit_Belgium$tidy$p.value[str_detect(ms_object_figure_3_Cit_Belgium$tidy$term, "^Politician text")]) < 0.05)*100

# TABLE N15
modelsummary(list("Politicians" = ms_object_figure_3_Pol_Pooled,
                  "Citizens" = ms_object_figure_3_Cit_Pooled,
                  "Politicians" = ms_object_figure_3_Pol_US,
                  "Citizens" = ms_object_figure_3_Cit_US,
                  "Politicians" = ms_object_figure_3_Pol_Denmark,
                  "Citizens" = ms_object_figure_3_Cit_Denmark,
                  "Politicians" = ms_object_figure_3_Pol_Chile,
                  "Citizens" = ms_object_figure_3_Cit_Chile,
                  "Politicians" = ms_object_figure_3_Pol_Belgium,
                  "Citizens" = ms_object_figure_3_Cit_Belgium),
             coef_map = c("Woman politician", "Gendered text", "Man user", "Co-partisan",
                          paste0("Politician text ", 1:20),
                          paste0("User text ", 1:20)),
             stars = c("*" = .05, "**" = .01, "***" = .001),
             output = "latex")



# FIGURE 4
gendered_interaction_formula <- chose_profile ~ text_pol + text_user +
                                                party_pol_copartisan + woman_user +
                                                woman_pol + gendered +
                                                woman_pol_gendered

user_gender_interaction_formula <- chose_profile ~ text_pol + text_user +
                                                   party_pol_copartisan + woman_user +
                                                   woman_pol + gendered +
                                                   woman_pol_man_user

G2_A <- paired_conjoint(filter(C, respondent_type == "Citizen"),
                        filter(C, respondent_type == "Politician"),
                        label1 = "Citizen sample", label2 = "Politician sample",
                        group = "Gender of politician & gendered message",
                        formula = gendered_interaction_formula)

G2_B <- paired_conjoint(filter(C, respondent_type == "Citizen"),
                        filter(C, respondent_type == "Politician"),
                        label1 = "Citizen sample", label2 = "Politician sample",
                        group = "Gender of politician & user",
                        formula = user_gender_interaction_formula)

# p-values for the interaction woman politician x gendered text
# 8 %-points, p < 0.001 (in both cases)
mean(G2_A$estimate[G2_A$level == "Woman politician\nx gendered text"])
G2_A[G2_A$level == "Woman politician\nx gendered text", ]

# p-values for the interaction woman politician x man user
# 4 %-points, p < 0.001 (in both cases)
mean(G2_B$estimate[G2_B$level == "Woman politician\nx man user"])
G2_B[G2_B$level == "Woman politician\nx man user", ]


# Same sample sizes in each panel, because same data; different interactions
# FIGURE 4 figure note
cat(paste0("\n\nCitizen sample observations~$=$~",
           scales::comma(unique(G2_A$n[G2_A$respondent_type == "Citizen sample"])),
           " (",
           scales::comma(unique(G2_A$n_clusters[G2_A$respondent_type == "Citizen sample"])),
           " respondents); politician sample observations~$=$~",
           scales::comma(unique(G2_A$n[G2_A$respondent_type == "Politician sample"])),
           " (",
           scales::comma(unique(G2_A$n_clusters[G2_A$respondent_type == "Politician sample"])),
           " respondents).\n\n"))


Graph2_A <- ggplot(filter(G2_A, feature %in% c("Gender of politician", "Gendered text", "Interaction")),
                   aes(x = estimate, y = level, yend = level,
                       color = feature, shape = respondent_type)) +
     my.theme(base_size = 7, borderless = 2,
              remove.ticks.y = TRUE, remove.ticks.x = TRUE,
              grid.y_colour = "grey65", grid.y_linetype = 3,
              grid.x_colour = "grey85", grid.x_linetype = 1) +
     labs(x = "AMCE (in percentage points)", y = "") +
     ggforce::facet_col( ~ feature, scales = "free_y", space = "free") +
     coord_cartesian(xlim = c(-0.03, 0.12)) +
     scale_x_continuous(breaks = seq(-0.02, 0.12, by = 0.02),
                        labels = c("-2", "0", "2", "4", "6", "8", "10", "12")) +
     geom_vline(xintercept = 0, linetype = 1, size = 0.35, color = "grey50") +
     geom_point(size = 0, color = "transparent") +
     geom_segment(data = filter(G2_A, feature %in% c("Gender of politician", "Gendered text", "Interaction") & !is.na(std.error) & respondent_type == "Citizen sample"),
                  aes(x = lower, xend = upper),
                  size = 0.4, position = position_nudge(x = 0, y = 0.2)) +
     geom_point(data = filter(G2_A, feature %in% c("Gender of politician", "Gendered text", "Interaction") & !is.na(std.error) & respondent_type == "Citizen sample"),
                size = 2.75, position = position_nudge(x = 0, y = 0.2),
                fill = "white", stroke = 1) +
     geom_segment(data = filter(G2_A, feature %in% c("Gender of politician", "Gendered text", "Interaction") & !is.na(std.error) & respondent_type == "Politician sample"),
                  aes(x = lower, xend = upper),
                  size = 0.4, position = position_nudge(x = 0, y = -0.2)) +
     geom_point(data = filter(G2_A, feature %in% c("Gender of politician", "Gendered text", "Interaction") & !is.na(std.error) & respondent_type == "Politician sample"),
                size = 2.75, position = position_nudge(x = 0, y = -0.2),
                fill = "white", stroke = 1) +
     geom_point(data = filter(G2_A, feature %in% c("Gender of politician", "Gendered text", "Interaction") & is.na(std.error) & respondent_type == "Politician sample"),
                size = 2, position = position_nudge(x = 0, y = 0),
                color = "black", fill = "black") +
     geom_label(data = filter(G2_A, feature == "Interaction"),
          aes(x = lower, y = level, label = respondent_type),
          hjust = 1.05, vjust = 0.3, fontface = "bold",
          label.padding = unit(0.1, "lines"), label.size = NA,
               label.r = unit(0.15, "lines"),
          position = position_dodge(width = -0.8), size = 2.5, alpha = 0.7) +
     scale_color_manual(values = c("#5200FF", "#00bd8e", "#FF3E00", "#A1B811")) +
     scale_shape_manual(values = c(16, 21)) +
     theme(legend.position = "none",
           strip.text.x = element_text(angle = 0, hjust = 0, size = 7))

Graph2_B <- ggplot(filter(G2_B, feature %in% c("Gender of politician", "Gender of user", "Interaction")),
                   aes(x = estimate, y = level, yend = level,
                       color = feature, shape = respondent_type)) +
     my.theme(base_size = 7, borderless = 2,
              remove.ticks.y = TRUE, remove.ticks.x = TRUE,
              grid.y_colour = "grey65", grid.y_linetype = 3,
              grid.x_colour = "grey85", grid.x_linetype = 1) +
     labs(x = "AMCE (in percentage points)", y = "") +
     ggforce::facet_col( ~ feature, scales = "free_y", space = "free") +
     coord_cartesian(xlim = c(-0.03, 0.12)) +
     scale_x_continuous(breaks = seq(-0.02, 0.12, by = 0.02),
                        labels = c("-2", "0", "2", "4", "6", "8", "10", "12")) +
     geom_vline(xintercept = 0, linetype = 1, size = 0.35, color = "grey50") +
     geom_point(size = 0, color = "transparent") +
     geom_segment(data = filter(G2_B, feature %in% c("Gender of politician", "Gender of user", "Interaction") & !is.na(std.error) & respondent_type == "Citizen sample"),
                  aes(x = lower, xend = upper),
                  size = 0.4, position = position_nudge(x = 0, y = 0.2)) +
     geom_point(data = filter(G2_B, feature %in% c("Gender of politician", "Gender of user", "Interaction") & !is.na(std.error) & respondent_type == "Citizen sample"),
                size = 2.75, position = position_nudge(x = 0, y = 0.2),
                fill = "white", stroke = 1) +
     geom_segment(data = filter(G2_B, feature %in% c("Gender of politician", "Gender of user", "Interaction") & !is.na(std.error) & respondent_type == "Politician sample"),
                  aes(x = lower, xend = upper),
                  size = 0.4, position = position_nudge(x = 0, y = -0.2)) +
     geom_point(data = filter(G2_B, feature %in% c("Gender of politician", "Gender of user", "Interaction") & !is.na(std.error) & respondent_type == "Politician sample"),
                size = 2.75, position = position_nudge(x = 0, y = -0.2),
                fill = "white", stroke = 1) +
     geom_point(data = filter(G2_B, feature %in% c("Gender of politician", "Gender of user", "Interaction") & is.na(std.error) & respondent_type == "Politician sample"),
                size = 2, position = position_nudge(x = 0, y = 0),
                color = "black", fill = "black") +
     scale_color_manual(values = c("#5200FF", "#00bd8e", "#FF3E00", "#A1B811")) +
     scale_shape_manual(values = c(16, 21)) +
     theme(legend.position = "none",
           strip.text.x = element_text(angle = 0, hjust = 0, size = 7))

# FIGURE 4
pdf("Figures/Figure_4-RAW.pdf", 7, 3.75)
ggdraw() +
  draw_plot(Graph2_A, x = -0.02, y = 0.01, width = 0.51, height = 0.91) +
  draw_plot(Graph2_B, x = 0.48, y = 0.01, width = 0.51, height = 0.91) +
  draw_plot_label(label = c("A.",
                            "B."),
                  x = c(0.01, 0.51), y = c(0.992, 0.992),
                  size = 8, hjust = 0, vjust = 1) +
  draw_plot_label(label = c("Does the effect of a politician's gender depend on\nwhether his/her gender is mentioned in the toxic message?",
                            "Does the effect of a politician's gender depend on\nwhether the user sending the toxic message is male?"),
                  x = c(0.03, 0.53), y = c(0.992, 0.992),
                  fontface = "plain",
                  size = 8, hjust = 0, vjust = 1)
dev.off()


# Complete regression results for Figure 4
ms_object_figure_5A_Pol <- get_regression_table(filter(G2_A, respondent_type == "Politician sample"))
ms_object_figure_5A_Cit <- get_regression_table(filter(G2_A, respondent_type == "Citizen sample"))

ms_object_figure_5B_Pol <- get_regression_table(filter(G2_B, respondent_type == "Politician sample"))
ms_object_figure_5B_Cit <- get_regression_table(filter(G2_B, respondent_type == "Citizen sample"))

# TABLE N16
modelsummary(list("Politicians" = ms_object_figure_5A_Pol,
                  "Citizens" = ms_object_figure_5A_Cit,
                  "Politicians" = ms_object_figure_5B_Pol,
                  "Citizens" = ms_object_figure_5B_Cit),
             coef_map = c(setNames("Woman politician $\\times$ gendered text", "Woman politician\nx gendered text"),
                          setNames("Woman politician $\\times$ man user", "Woman politician\nx man user"),
                          setNames("Woman politician", "Woman politician"),
                          setNames("Gendered text", "Gendered text"),
                          setNames("Man user", "Man user"),
                          setNames("Co-partisan", "Co-partisan"),
                          setNames(paste0("Politician text ", 1:20), paste0("Politician text ", 1:20)),
                          setNames(paste0("User text ", 1:16), paste0("User text ", 1:16))),
             stars = c("*" = .05, "**" = .01, "***" = .001),
             output = "latex",
             escape = FALSE)



# Gendered text x woman politician (by country)
G2A_country <- bind_rows(tibble(paired_conjoint(filter(C, country == "Belgium" & respondent_type == "Citizen"),
                                                filter(C, country == "Belgium" & respondent_type == "Politician"),
                                                label1 = "Citizen sample", label2 = "Politician sample",
                                                group = "Gender of politician & gendered message",
                                                formula = gendered_interaction_formula),
                                country = "Belgium"),
                         tibble(paired_conjoint(filter(C, country == "Chile" & respondent_type == "Citizen"),
                                                filter(C, country == "Chile" & respondent_type == "Politician"),
                                                label1 = "Citizen sample", label2 = "Politician sample",
                                                group = "Gender of politician & gendered message",
                                                formula = gendered_interaction_formula),
                                country = "Chile"),
                         tibble(paired_conjoint(filter(C, country == "Denmark" & respondent_type == "Citizen"),
                                                filter(C, country == "Denmark" & respondent_type == "Politician"),
                                                label1 = "Citizen sample", label2 = "Politician sample",
                                                group = "Gender of politician & gendered message",
                                                formula = gendered_interaction_formula),
                                country = "Denmark"),
                         tibble(paired_conjoint(filter(C, country == "United States" & respondent_type == "Citizen"),
                                                filter(C, country == "United States" & respondent_type == "Politician"),
                                                label1 = "Citizen sample", label2 = "Politician sample",
                                                group = "Gender of politician & gendered message",
                                                formula = gendered_interaction_formula),
                                country = "US"))

G2A_country <- bind_rows(G2A_country, tibble(G2_A, country = "Pooled estimate"))
G2A_country <- filter(G2A_country, feature %in% c("Gender of politician", "Gendered text", "Interaction"))
G2A_country$country <- factor(G2A_country$country, levels = c("Belgium", "Chile", "Pooled estimate", "Denmark", "US"))

G2A_country_citizen <- ggplot(filter(G2A_country, respondent_type == "Citizen sample"),
       aes(x = estimate, xend = estimate, y = level, yend = level,
           color = country, size = country,
           shape = country, fill = country)) +
     my.theme(base_size = 7, borderless = 2, remove.ticks.y = TRUE, remove.ticks.x = TRUE,
              grid.y_colour = "grey65", grid.y_linetype = 3,
              grid.x_colour = "grey85", grid.x_linetype = 1) +
     labs(x = "AMCE (in percentage points)", y = "") +
     ggforce::facet_col( ~ feature, scales = "free_y", space = "free") +
     coord_cartesian(xlim = c(-0.03, 0.13)) +
     scale_x_continuous(breaks = seq(-0.06, 0.16, by = 0.02),
                        labels = c("-6", "-4", "-2", "0", "2", "4", "6", "8", "10", "12", "14", "16")) +
     geom_vline(xintercept = 0, linetype = 1, size = 0.35, color = "grey50") +
     geom_point(size = 0, color = "transparent") +
     geom_linerange(data = filter(G2A_country, !is.na(std.error) & respondent_type == "Citizen sample"),
                    aes(xmin = lower, xmax = upper),
                    position = position_dodge2(width = 1),
                    size = 0.5, show.legend = FALSE) +
     geom_point(data = filter(G2A_country, !is.na(std.error) & respondent_type == "Citizen sample"),
                position = position_dodge2(width = 1), stroke = 0.8) +
     geom_point(data = filter(G2A_country, country == "Pooled estimate", !is.na(std.error) & respondent_type == "Citizen sample"),
                position = position_dodge2(width = 1), stroke = 0.8) +
     geom_point(data = filter(G2A_country, country == "Pooled estimate" & is.na(std.error) & respondent_type == "Citizen sample"),
                size = 2, position = position_nudge(x = 0, y = 0),
                color = "black", shape = 16, show.legend = FALSE) +
     scale_size_manual(values = c(0.75, 0.75, 2, 0.75, 0.75)) +
     scale_shape_manual(values = c(21, 21, 23, 21, 21)) +
     scale_fill_manual(values = c("#00bd8e", "#FF3E00", "white", "#A1B811", "#5200FF")) +
     scale_colour_manual(values = c("#00bd8e", "#FF3E00", "black", "#A1B811", "#5200FF")) +
     theme(legend.position = "top",
           strip.text.x = element_text(angle = 0, hjust = 0, size = 7, face = "plain")) +
     guides(size = guide_legend(override.aes = list(size = 2)))

G2A_country_politician <- ggplot(filter(G2A_country, respondent_type == "Politician sample"),
       aes(x = estimate, xend = estimate, y = level, yend = level,
           color = country, size = country,
           shape = country, fill = country)) +
     my.theme(base_size = 7, borderless = 2, remove.ticks.y = TRUE, remove.ticks.x = TRUE,
              grid.y_colour = "grey65", grid.y_linetype = 3,
              grid.x_colour = "grey85", grid.x_linetype = 1) +
     labs(x = "AMCE (in percentage points)", y = "") +
     ggforce::facet_col( ~ feature, scales = "free_y", space = "free") +
     coord_cartesian(xlim = c(-0.03, 0.13)) +
     scale_x_continuous(breaks = seq(-0.06, 0.16, by = 0.02),
                        labels = c("-6", "-4", "-2", "0", "2", "4", "6", "8", "10", "12", "14", "16")) +
     geom_vline(xintercept = 0, linetype = 1, size = 0.35, color = "grey50") +
     geom_point(size = 0, color = "transparent") +
     geom_linerange(data = filter(G2A_country, !is.na(std.error) & respondent_type == "Politician sample"),
                    aes(xmin = lower, xmax = upper),
                    position = position_dodge2(width = 1),
                    size = 0.5, show.legend = FALSE) +
     geom_point(data = filter(G2A_country, !is.na(std.error) & respondent_type == "Politician sample"),
                position = position_dodge2(width = 1), stroke = 0.8) +
     geom_point(data = filter(G2A_country, country == "Pooled estimate", !is.na(std.error) & respondent_type == "Politician sample"),
                position = position_dodge2(width = 1), stroke = 0.8) +
     geom_point(data = filter(G2A_country, country == "Pooled estimate" & is.na(std.error) & respondent_type == "Politician sample"),
                size = 2, position = position_nudge(x = 0, y = 0),
                color = "black", shape = 16, show.legend = FALSE) +
     scale_size_manual(values = c(0.75, 0.75, 2, 0.75, 0.75)) +
     scale_shape_manual(values = c(21, 21, 23, 21, 21)) +
     scale_fill_manual(values = c("#00bd8e", "#FF3E00", "white", "#A1B811", "#5200FF")) +
     scale_colour_manual(values = c("#00bd8e", "#FF3E00", "black", "#A1B811", "#5200FF")) +
     theme(legend.position = "top",
           strip.text.x = element_text(angle = 0, hjust = 0, size = 7, face = "plain")) +
     guides(size = guide_legend(override.aes = list(size = 2)))


# FIGURE E2
pdf("Figures/Main_Interactions_A_Country-RAW.pdf", 7, 4)
ggdraw() +
  draw_plot(G2A_country_citizen, x = -0.01, y = 0.01, width = 0.5, height = 1) +
  draw_plot(G2A_country_politician, x = 0.48, y = 0.01, width = 0.5, height = 1) +
  draw_plot_label(label = c("A. Citizen sample",
                            "B. Politician sample"),
                  x = c(0.01, 0.51), y = c(0.992, 0.992),
                  size = 8, hjust = 0, vjust = 1)
dev.off()



# Male user x woman politician (by country)
G2B_country <- bind_rows(tibble(paired_conjoint(filter(C, country == "Belgium" & respondent_type == "Citizen"),
                                                filter(C, country == "Belgium" & respondent_type == "Politician"),
                                                label1 = "Citizen sample", label2 = "Politician sample",
                                                group = "Gender of politician & user",
                                                formula = user_gender_interaction_formula),
                                country = "Belgium"),
                         tibble(paired_conjoint(filter(C, country == "Chile" & respondent_type == "Citizen"),
                                                filter(C, country == "Chile" & respondent_type == "Politician"),
                                                label1 = "Citizen sample", label2 = "Politician sample",
                                                group = "Gender of politician & user",
                                                formula = user_gender_interaction_formula),
                                country = "Chile"),
                         tibble(paired_conjoint(filter(C, country == "Denmark" & respondent_type == "Citizen"),
                                                filter(C, country == "Denmark" & respondent_type == "Politician"),
                                                label1 = "Citizen sample", label2 = "Politician sample",
                                                group = "Gender of politician & user",
                                                formula = user_gender_interaction_formula),
                                country = "Denmark"),
                         tibble(paired_conjoint(filter(C, country == "United States" & respondent_type == "Citizen"),
                                                filter(C, country == "United States" & respondent_type == "Politician"),
                                                label1 = "Citizen sample", label2 = "Politician sample",
                                                group = "Gender of politician & user",
                                                formula = user_gender_interaction_formula),
                                country = "US"))

G2B_country <- bind_rows(G2B_country, tibble(G2_B, country = "Pooled estimate"))
G2B_country <- filter(G2B_country, feature %in% c("Gender of politician", "Gender of user", "Interaction"))
G2B_country$country <- factor(G2B_country$country, levels = c("Belgium", "Chile", "Pooled estimate", "Denmark", "US"))

G2B_country_citizen <- ggplot(filter(G2B_country, respondent_type == "Citizen sample"),
       aes(x = estimate, xend = estimate, y = level, yend = level,
           color = country, size = country,
           shape = country, fill = country)) +
     my.theme(base_size = 7, borderless = 2, remove.ticks.y = TRUE, remove.ticks.x = TRUE,
              grid.y_colour = "grey65", grid.y_linetype = 3,
              grid.x_colour = "grey85", grid.x_linetype = 1) +
     labs(x = "AMCE (in percentage points)", y = "") +
     ggforce::facet_col( ~ feature, scales = "free_y", space = "free") +
     coord_cartesian(xlim = c(-0.06, 0.10)) +
     scale_x_continuous(breaks = seq(-0.08, 0.16, by = 0.02),
                        labels = c("-8", "-6", "-4", "-2", "0", "2", "4", "6", "8", "10", "12", "14", "16")) +
     geom_vline(xintercept = 0, linetype = 1, size = 0.35, color = "grey50") +
     geom_point(size = 0, color = "transparent") +
     geom_linerange(data = filter(G2B_country, !is.na(std.error) & respondent_type == "Citizen sample"),
                    aes(xmin = lower, xmax = upper),
                    position = position_dodge2(width = 1),
                    size = 0.5, show.legend = FALSE) +
     geom_point(data = filter(G2B_country, !is.na(std.error) & respondent_type == "Citizen sample"),
                position = position_dodge2(width = 1), stroke = 0.8) +
     geom_point(data = filter(G2B_country, country == "Pooled estimate", !is.na(std.error) & respondent_type == "Citizen sample"),
                position = position_dodge2(width = 1), stroke = 0.8) +
     geom_point(data = filter(G2B_country, country == "Pooled estimate" & is.na(std.error) & respondent_type == "Citizen sample"),
                size = 2, position = position_nudge(x = 0, y = 0),
                color = "black", shape = 16, show.legend = FALSE) +
     scale_size_manual(values = c(0.75, 0.75, 2, 0.75, 0.75)) +
     scale_shape_manual(values = c(21, 21, 23, 21, 21)) +
     scale_fill_manual(values = c("#00bd8e", "#FF3E00", "white", "#A1B811", "#5200FF")) +
     scale_colour_manual(values = c("#00bd8e", "#FF3E00", "black", "#A1B811", "#5200FF")) +
     theme(legend.position = "top",
           strip.text.x = element_text(angle = 0, hjust = 0, size = 7, face = "plain")) +
     guides(size = guide_legend(override.aes = list(size = 2)))

G2B_country_politician <- ggplot(filter(G2B_country, respondent_type == "Politician sample"),
       aes(x = estimate, xend = estimate, y = level, yend = level,
           color = country, size = country,
           shape = country, fill = country)) +
     my.theme(base_size = 7, borderless = 2, remove.ticks.y = TRUE, remove.ticks.x = TRUE,
              grid.y_colour = "grey65", grid.y_linetype = 3,
              grid.x_colour = "grey85", grid.x_linetype = 1) +
     labs(x = "AMCE (in percentage points)", y = "") +
     ggforce::facet_col( ~ feature, scales = "free_y", space = "free") +
     coord_cartesian(xlim = c(-0.06, 0.10)) +
     scale_x_continuous(breaks = seq(-0.08, 0.16, by = 0.02),
                        labels = c("-8", "-6", "-4", "-2", "0", "2", "4", "6", "8", "10", "12", "14", "16")) +
     geom_vline(xintercept = 0, linetype = 1, size = 0.35, color = "grey50") +
     geom_point(size = 0, color = "transparent") +
     geom_linerange(data = filter(G2B_country, !is.na(std.error) & respondent_type == "Politician sample"),
                    aes(xmin = lower, xmax = upper),
                    position = position_dodge2(width = 1),
                    size = 0.5, show.legend = FALSE) +
     geom_point(data = filter(G2B_country, !is.na(std.error) & respondent_type == "Politician sample"),
                position = position_dodge2(width = 1), stroke = 0.8) +
     geom_point(data = filter(G2B_country, country == "Pooled estimate", !is.na(std.error) & respondent_type == "Politician sample"),
                position = position_dodge2(width = 1), stroke = 0.8) +
     geom_point(data = filter(G2B_country, country == "Pooled estimate" & is.na(std.error) & respondent_type == "Politician sample"),
                size = 2, position = position_nudge(x = 0, y = 0),
                color = "black", shape = 16, show.legend = FALSE) +
     scale_size_manual(values = c(0.75, 0.75, 2, 0.75, 0.75)) +
     scale_shape_manual(values = c(21, 21, 23, 21, 21)) +
     scale_fill_manual(values = c("#00bd8e", "#FF3E00", "white", "#A1B811", "#5200FF")) +
     scale_colour_manual(values = c("#00bd8e", "#FF3E00", "black", "#A1B811", "#5200FF")) +
     theme(legend.position = "top",
           strip.text.x = element_text(angle = 0, hjust = 0, size = 7, face = "plain")) +
     guides(size = guide_legend(override.aes = list(size = 2)))

# FIGURE E3
pdf("Figures/Main_Interactions_B_Country-RAW.pdf", 7, 4)
ggdraw() +
  draw_plot(G2B_country_citizen, x = -0.01, y = 0.01, width = 0.5, height = 1) +
  draw_plot(G2B_country_politician, x = 0.48, y = 0.01, width = 0.5, height = 1) +
  draw_plot_label(label = c("A. Citizen sample",
                            "B. Politician sample"),
                  x = c(0.01, 0.51), y = c(0.992, 0.992),
                  size = 8, hjust = 0, vjust = 1)
dev.off()





# FIGURE 5
G3_A_Politician <- paired_conjoint(filter(C, resp_gender == "Female" & respondent_type == "Politician"),
                                   filter(C, resp_gender == "Male" & respondent_type == "Politician"),
                                   label1 = "Woman",
                                   label2 = "Man",
                                   group = "Respondent gender")
G3_A_Citizen <- paired_conjoint(filter(C, resp_gender == "Female" & respondent_type == "Citizen"),
                                filter(C, resp_gender == "Male" & respondent_type == "Citizen"),
                                label1 = "Woman",
                                label2 = "Man",
                                group = "Respondent gender")

G3_B_Politician <- paired_conjoint(filter(C, resp_ideology_discrete == "Right-wing" & respondent_type == "Politician"),
                                   filter(C, resp_ideology_discrete == "Left-wing" & respondent_type == "Politician"),
                                   label1 = "Right-wing",
                                   label2 = "Left-wing",
                                   group = "Respondent ideology")
G3_B_Citizen <- paired_conjoint(filter(C, resp_ideology_discrete == "Right-wing" & respondent_type == "Citizen"),
                                filter(C, resp_ideology_discrete == "Left-wing" & respondent_type == "Citizen"),
                                label1 = "Right-wing",
                                label2 = "Left-wing",
                                group = "Respondent ideology")

co_partisan_formula <- chose_profile ~ text_pol + text_user + gendered +
                                       woman_pol + woman_user
G3_C_Politician <- paired_conjoint(filter(C, party_pol_copartisan == "Co-partisan" & respondent_type == "Politician"),
                                   filter(C, party_pol_copartisan == "Non co-partisan" & respondent_type == "Politician"),
                                   label1 = "Co-partisan",
                                   label2 = "Non co-partisan",
                                   group = "Respondent is a co-partisan\nof the politician",
                                   formula = co_partisan_formula)
G3_C_Citizen <- paired_conjoint(filter(C, party_pol_copartisan == "Co-partisan" & respondent_type == "Citizen"),
                                   filter(C, party_pol_copartisan == "Non co-partisan" & respondent_type == "Citizen"),
                                   label1 = "Co-partisan",
                                   label2 = "Non co-partisan",
                                   group = "Respondent is a co-partisan\nof the politician",
                                   formula = co_partisan_formula)

G3_D_Politician <- paired_conjoint(filter(C, resp_exposure_binary == "Experienced social media harassment" & respondent_type == "Politician"),
                        filter(C, resp_exposure_binary == "Has not experienced social media harassment" & respondent_type == "Politician"),
                        label1 = "Exp. w/ harassment",
                        label2 = "No exp. w/ harassment",
                        group = "Respondent experience with\nsocial media harassment")
G3_D_Citizen <- paired_conjoint(filter(C, resp_exposure_binary == "Experienced social media harassment" & respondent_type == "Citizen"),
                        filter(C, resp_exposure_binary == "Has not experienced social media harassment" & respondent_type == "Citizen"),
                        label1 = "Exp. w/ harassment",
                        label2 = "No exp. w/ harassment",
                        group = "Respondent experience with\nsocial media harassment")

G3_Split <- bind_rows(tibble(filter(G3_A_Politician, feature == "Gender of politician"), respondent_sample = "Politician"),
                      tibble(filter(G3_A_Citizen, feature == "Gender of politician"), respondent_sample = "Citizen"),
                      tibble(filter(G3_B_Politician, feature == "Gender of politician"), respondent_sample = "Politician"),
                      tibble(filter(G3_B_Citizen, feature == "Gender of politician"), respondent_sample = "Citizen"),
                      tibble(filter(G3_C_Politician, feature == "Gender of politician"), respondent_sample = "Politician"),
                      tibble(filter(G3_C_Citizen, feature == "Gender of politician"), respondent_sample = "Citizen"),
                      tibble(filter(G3_D_Politician, feature == "Gender of politician"), respondent_sample = "Politician"),
                      tibble(filter(G3_D_Citizen, feature == "Gender of politician"), respondent_sample = "Citizen"))

G3_Split <- filter(G3_Split, !is.na(std.error))

G3_Split$group <- factor(G3_Split$group, levels = c("Respondent gender",
                                                    "Respondent ideology",
                                                    "Respondent is a co-partisan\nof the politician",
                                                    "Respondent experience with\nsocial media harassment"))


G3A_Split <- ggplot(filter(G3_Split, respondent_sample == "Politician"),
       aes(x = estimate, y = respondent_type, yend = respondent_type,
           color = group)) +
     my.theme(base_size = 8, borderless = 2,
              remove.ticks.y = TRUE, remove.ticks.x = TRUE,
              grid.y_colour = "grey65", grid.y_linetype = 3,
              grid.x_colour = "grey85", grid.x_linetype = 1) +
     labs(x = "AMCE (in percentage points)\nfor whether the politician is a woman",
          y = "") +
     ggforce::facet_col( ~ group, scales = "free_y", space = "free") +
     coord_cartesian(xlim = c(-0.01, 0.10)) +
     scale_x_continuous(breaks = seq(0, 0.14, by = 0.02),
                        labels = c("0", "2", "4", "6", "8", "10", "12", "14")) +
     geom_vline(xintercept = 0, linetype = 1, size = 0.35, color = "grey50") +
     geom_point(size = 0, color = "transparent") +
     geom_point(data = filter(G3_Split, respondent_sample == "Politician"),
                size = 3) +
     geom_segment(data = filter(G3_Split, respondent_sample == "Politician"), aes(x = lower, xend = upper), size = 0.4) +
     scale_colour_manual(values = c("#5200FF", "#00bd8e", "#FF3E00", "#A1B811")) +
     theme(legend.position = "none",
           strip.text.x = element_text(face = "bold", angle = 0, hjust = 0, size = 8))

G3B_Split <- ggplot(filter(G3_Split, respondent_sample == "Citizen"),
       aes(x = estimate, y = respondent_type, yend = respondent_type,
           color = group)) +
     my.theme(base_size = 8, borderless = 2,
              remove.ticks.y = TRUE, remove.ticks.x = TRUE,
              grid.y_colour = "grey65", grid.y_linetype = 3,
              grid.x_colour = "grey85", grid.x_linetype = 1) +
     labs(x = "AMCE (in percentage points)\nfor whether the politician is a woman",
          y = "") +
     ggforce::facet_col( ~ group, scales = "free_y", space = "free") +
     coord_cartesian(xlim = c(-0.01, 0.10)) +
     scale_x_continuous(breaks = seq(0, 0.14, by = 0.02),
                        labels = c("0", "2", "4", "6", "8", "10", "12", "14")) +
     scale_y_discrete(labels = c("", "", "", "", "", "", "", "")) +
     geom_vline(xintercept = 0, linetype = 1, size = 0.35, color = "grey50") +
     geom_point(size = 0, color = "transparent") +
     geom_point(data = filter(G3_Split, respondent_sample == "Citizen"),
                size = 3) +
     geom_segment(data = filter(G3_Split, respondent_sample == "Citizen"), aes(x = lower, xend = upper), size = 0.4) +
     scale_colour_manual(values = c("#5200FF", "#00bd8e", "#FF3E00", "#A1B811")) +
     theme(legend.position = "none",
           strip.text.x = element_text(face = "bold", angle = 0, hjust = 0, size = 8))


cat(paste0("\n\nAverage politician sample observations per sub-group~$=$~",
           scales::comma(mean(unique(G3_Split$n[G3_Split$respondent_sample == "Politician"]))),
           " (",
           scales::comma(mean(unique(G3_Split$n_clusters[G3_Split$respondent_sample == "Politician"]))),
           " respondents); avg. citizen sample observations per sub-group~$=$~",
           scales::comma(mean(unique(G3_Split$n[G3_Split$respondent_sample == "Citizen"]))),
           " (",
           scales::comma(mean(unique(G3_Split$n_clusters[G3_Split$respondent_sample == "Citizen"]))),
           " respondents).\n\n"))

# FIGURE 5
pdf("Figures/Figure_5-RAW.pdf", 7.5, 4.75)
ggdraw() +
  draw_plot(G3A_Split, x = -0.02, y = 0.01, width = 0.62, height = 0.96) +
  draw_plot(G3B_Split, x = 0.59, y = 0.01, width = 0.40, height = 0.96) +
  draw_plot_label(label = c("A.",
                            "B."),
                  x = c(0.01, 0.60), y = c(0.992, 0.992),
                  size = 8, hjust = 0, vjust = 1) +
  draw_plot_label(label = c("Politician sample",
                            "Citizen sample"),
                  x = c(0.03, 0.62), y = c(0.992, 0.992),
                  fontface = "plain",
                  size = 8, hjust = 0, vjust = 1)
dev.off()


# Complete regression results for Figure 5

# Gender
ms_object_figure_5_G1_Pol <- get_regression_table(filter(G3_A_Politician, respondent_type == "Woman"))
ms_object_figure_5_G2_Pol <- get_regression_table(filter(G3_A_Politician, respondent_type == "Man"))
ms_object_figure_5_G1_Cit <- get_regression_table(filter(G3_A_Citizen, respondent_type == "Woman"))
ms_object_figure_5_G2_Cit <- get_regression_table(filter(G3_A_Citizen, respondent_type == "Man"))
# Ideology
ms_object_figure_5_G3_Pol <- get_regression_table(filter(G3_B_Politician, respondent_type == "Right-wing"))
ms_object_figure_5_G4_Pol <- get_regression_table(filter(G3_B_Politician, respondent_type == "Left-wing"))
ms_object_figure_5_G3_Cit <- get_regression_table(filter(G3_B_Citizen, respondent_type == "Right-wing"))
ms_object_figure_5_G4_Cit <- get_regression_table(filter(G3_B_Citizen, respondent_type == "Left-wing"))
# Ideology
ms_object_figure_5_G5_Pol <- get_regression_table(filter(G3_C_Politician, respondent_type == "Co-partisan"))
ms_object_figure_5_G6_Pol <- get_regression_table(filter(G3_C_Politician, respondent_type == "Non co-partisan"))
ms_object_figure_5_G5_Cit <- get_regression_table(filter(G3_C_Citizen, respondent_type == "Co-partisan"))
ms_object_figure_5_G6_Cit <- get_regression_table(filter(G3_C_Citizen, respondent_type == "Non co-partisan"))
# Exposure
ms_object_figure_5_G7_Pol <- get_regression_table(filter(G3_D_Politician, respondent_type == "Exp. w/ harassment"))
ms_object_figure_5_G8_Pol <- get_regression_table(filter(G3_D_Politician, respondent_type == "No exp. w/ harassment"))
ms_object_figure_5_G7_Cit <- get_regression_table(filter(G3_D_Citizen, respondent_type == "Exp. w/ harassment"))
ms_object_figure_5_G8_Cit <- get_regression_table(filter(G3_D_Citizen, respondent_type == "No exp. w/ harassment"))


# TABLE N17
# Politician sample
modelsummary(list("Women" = ms_object_figure_5_G1_Pol,
                  "Men" = ms_object_figure_5_G2_Pol,
                  "Right-wing" = ms_object_figure_5_G3_Pol,
                  "Left-wing" = ms_object_figure_5_G4_Pol,
                  "Co-partisan" = ms_object_figure_5_G5_Pol,
                  "Out-partisan" = ms_object_figure_5_G6_Pol,
                  "Exp. with harassment" = ms_object_figure_5_G7_Pol,
                  "No exp. with harassment" = ms_object_figure_5_G8_Pol),
             coef_map = c(setNames("Woman politician", "Woman politician"),
                          setNames("Gendered text", "Gendered text"),
                          setNames("Man user", "Man user"),
                          setNames("Co-partisan", "Co-partisan"),
                          setNames(paste0("Politician text ", 1:20), paste0("Politician text ", 1:20)),
                          setNames(paste0("User text ", 1:16), paste0("User text ", 1:16))),
             stars = c("*" = .05, "**" = .01, "***" = .001),
             output = "latex",
             escape = FALSE)

# TABLE N18
# Citizen sample
modelsummary(list("Women" = ms_object_figure_5_G1_Cit,
                  "Men" = ms_object_figure_5_G2_Cit,
                  "Right-wing" = ms_object_figure_5_G3_Cit,
                  "Left-wing" = ms_object_figure_5_G4_Cit,
                  "Co-partisan" = ms_object_figure_5_G5_Cit,
                  "Out-partisan" = ms_object_figure_5_G6_Cit,
                  "Exp. with harassment" = ms_object_figure_5_G7_Cit,
                  "No exp. with harassment" = ms_object_figure_5_G8_Cit),
             coef_map = c(setNames("Woman politician", "Woman politician"),
                          setNames("Gendered text", "Gendered text"),
                          setNames("Man user", "Man user"),
                          setNames("Co-partisan", "Co-partisan"),
                          setNames(paste0("Politician text ", 1:20), paste0("Politician text ", 1:20)),
                          setNames(paste0("User text ", 1:16), paste0("User text ", 1:16))),
             stars = c("*" = .05, "**" = .01, "***" = .001),
             output = "latex",
             escape = FALSE)




# FIGURE 5 BY COUNTRY FOR APPENDIX
G3_A_Politician_Country <- bind_rows(tibble(paired_conjoint(filter(C, country == "Belgium" & resp_gender == "Female" & respondent_type == "Politician"),
                                                            filter(C, country == "Belgium" & resp_gender == "Male" & respondent_type == "Politician"),
                                                            label1 = "Woman", label2 = "Man", group = "Respondent gender"),
                                            country = "Belgium"),
                                     tibble(paired_conjoint(filter(C, country == "Chile" & resp_gender == "Female" & respondent_type == "Politician"),
                                                            filter(C, country == "Chile" & resp_gender == "Male" & respondent_type == "Politician"),
                                                            label1 = "Woman", label2 = "Man", group = "Respondent gender"),
                                            country = "Chile"),
                                     tibble(paired_conjoint(filter(C, country == "Denmark" & resp_gender == "Female" & respondent_type == "Politician"),
                                                            filter(C, country == "Denmark" & resp_gender == "Male" & respondent_type == "Politician"),
                                                            label1 = "Woman", label2 = "Man", group = "Respondent gender"),
                                            country = "Denmark"),
                                     tibble(paired_conjoint(filter(C, country == "United States" & resp_gender == "Female" & respondent_type == "Politician"),
                                                            filter(C, country == "United States" & resp_gender == "Male" & respondent_type == "Politician"),
                                                            label1 = "Woman", label2 = "Man", group = "Respondent gender"),
                                            country = "US"))

G3_A_Citizen_Country <- bind_rows(tibble(paired_conjoint(filter(C, country == "Belgium" & resp_gender == "Female" & respondent_type == "Citizen"),
                                                         filter(C, country == "Belgium" & resp_gender == "Male" & respondent_type == "Citizen"),
                                                         label1 = "Woman", label2 = "Man", group = "Respondent gender"),
                                         country = "Belgium"),
                                  tibble(paired_conjoint(filter(C, country == "Chile" & resp_gender == "Female" & respondent_type == "Citizen"),
                                                         filter(C, country == "Chile" & resp_gender == "Male" & respondent_type == "Citizen"),
                                                         label1 = "Woman", label2 = "Man", group = "Respondent gender"),
                                         country = "Chile"),
                                  tibble(paired_conjoint(filter(C, country == "Denmark" & resp_gender == "Female" & respondent_type == "Citizen"),
                                                         filter(C, country == "Denmark" & resp_gender == "Male" & respondent_type == "Citizen"),
                                                         label1 = "Woman", label2 = "Man", group = "Respondent gender"),
                                         country = "Denmark"),
                                  tibble(paired_conjoint(filter(C, country == "United States" & resp_gender == "Female" & respondent_type == "Citizen"),
                                                         filter(C, country == "United States" & resp_gender == "Male" & respondent_type == "Citizen"),
                                                         label1 = "Woman", label2 = "Man", group = "Respondent gender"),
                                         country = "US"))

G3_B_Politician_Country <- bind_rows(tibble(paired_conjoint(filter(C, country == "Belgium" & resp_ideology_discrete == "Right-wing" & respondent_type == "Politician"),
                                                            filter(C, country == "Belgium" & resp_ideology_discrete == "Left-wing" & respondent_type == "Politician"),
                                                            label1 = "Right-wing", label2 = "Left-wing", group = "Respondent ideology"),
                                            country = "Belgium"),
                                     tibble(paired_conjoint(filter(C, country == "Chile" & resp_ideology_discrete == "Right-wing" & respondent_type == "Politician"),
                                                            filter(C, country == "Chile" & resp_ideology_discrete == "Left-wing" & respondent_type == "Politician"),
                                                            label1 = "Right-wing", label2 = "Left-wing", group = "Respondent ideology"),
                                            country = "Chile"),
                                     tibble(paired_conjoint(filter(C, country == "Denmark" & resp_ideology_discrete == "Right-wing" & respondent_type == "Politician"),
                                                            filter(C, country == "Denmark" & resp_ideology_discrete == "Left-wing" & respondent_type == "Politician"),
                                                            label1 = "Right-wing", label2 = "Left-wing", group = "Respondent ideology"),
                                            country = "Denmark"),
                                     tibble(paired_conjoint(filter(C, country == "United States" & resp_ideology_discrete == "Right-wing" & respondent_type == "Politician"),
                                                            filter(C, country == "United States" & resp_ideology_discrete == "Left-wing" & respondent_type == "Politician"),
                                                            label1 = "Right-wing", label2 = "Left-wing", group = "Respondent ideology"),
                                            country = "US"))

G3_B_Citizen_Country <- bind_rows(tibble(paired_conjoint(filter(C, country == "Belgium" & resp_ideology_discrete == "Right-wing" & respondent_type == "Citizen"),
                                                         filter(C, country == "Belgium" & resp_ideology_discrete == "Left-wing" & respondent_type == "Citizen"),
                                                         label1 = "Right-wing", label2 = "Left-wing", group = "Respondent ideology"),
                                         country = "Belgium"),
                                  tibble(paired_conjoint(filter(C, country == "Chile" & resp_ideology_discrete == "Right-wing" & respondent_type == "Citizen"),
                                                         filter(C, country == "Chile" & resp_ideology_discrete == "Left-wing" & respondent_type == "Citizen"),
                                                         label1 = "Right-wing", label2 = "Left-wing", group = "Respondent ideology"),
                                         country = "Chile"),
                                  tibble(paired_conjoint(filter(C, country == "Denmark" & resp_ideology_discrete == "Right-wing" & respondent_type == "Citizen"),
                                                         filter(C, country == "Denmark" & resp_ideology_discrete == "Left-wing" & respondent_type == "Citizen"),
                                                         label1 = "Right-wing", label2 = "Left-wing", group = "Respondent ideology"),
                                         country = "Denmark"),
                                  tibble(paired_conjoint(filter(C, country == "United States" & resp_ideology_discrete == "Right-wing" & respondent_type == "Citizen"),
                                                         filter(C, country == "United States" & resp_ideology_discrete == "Left-wing" & respondent_type == "Citizen"),
                                                         label1 = "Right-wing", label2 = "Left-wing", group = "Respondent ideology"),
                                         country = "US"))

G3_C_Politician_Country <- bind_rows(tibble(paired_conjoint(filter(C, country == "Belgium" & party_pol_copartisan == "Co-partisan" & respondent_type == "Politician"),
                                                            filter(C, country == "Belgium" & party_pol_copartisan == "Non co-partisan" & respondent_type == "Politician"),
                                                            label1 = "Co-partisan", label2 = "Non co-partisan", group = "Respondent is a co-partisan\nof the politician",
                                                            formula = co_partisan_formula),
                                            country = "Belgium"),
                                     tibble(paired_conjoint(filter(C, country == "Chile" & party_pol_copartisan == "Co-partisan" & respondent_type == "Politician"),
                                                            filter(C, country == "Chile" & party_pol_copartisan == "Non co-partisan" & respondent_type == "Politician"),
                                                            label1 = "Co-partisan", label2 = "Non co-partisan", group = "Respondent is a co-partisan\nof the politician",
                                                            formula = co_partisan_formula),
                                            country = "Chile"),
                                     tibble(paired_conjoint(filter(C, country == "Denmark" & party_pol_copartisan == "Co-partisan" & respondent_type == "Politician"),
                                                            filter(C, country == "Denmark" & party_pol_copartisan == "Non co-partisan" & respondent_type == "Politician"),
                                                            label1 = "Co-partisan", label2 = "Non co-partisan", group = "Respondent is a co-partisan\nof the politician",
                                                            formula = co_partisan_formula),
                                            country = "Denmark"),
                                     tibble(paired_conjoint(filter(C, country == "United States" & party_pol_copartisan == "Co-partisan" & respondent_type == "Politician"),
                                                            filter(C, country == "United States" & party_pol_copartisan == "Non co-partisan" & respondent_type == "Politician"),
                                                            label1 = "Co-partisan", label2 = "Non co-partisan", group = "Respondent is a co-partisan\nof the politician",
                                                            formula = co_partisan_formula),
                                            country = "US"))


G3_C_Citizen_Country <- bind_rows(tibble(paired_conjoint(filter(C, country == "Belgium" & party_pol_copartisan == "Co-partisan" & respondent_type == "Citizen"),
                                                         filter(C, country == "Belgium" & party_pol_copartisan == "Non co-partisan" & respondent_type == "Citizen"),
                                                         label1 = "Co-partisan", label2 = "Non co-partisan", group = "Respondent is a co-partisan\nof the politician",
                                                         formula = co_partisan_formula),
                                            country = "Belgium"),
                                  tibble(paired_conjoint(filter(C, country == "Chile" & party_pol_copartisan == "Co-partisan" & respondent_type == "Citizen"),
                                                         filter(C, country == "Chile" & party_pol_copartisan == "Non co-partisan" & respondent_type == "Citizen"),
                                                         label1 = "Co-partisan", label2 = "Non co-partisan", group = "Respondent is a co-partisan\nof the politician",
                                                         formula = co_partisan_formula),
                                         country = "Chile"),
                                  tibble(paired_conjoint(filter(C, country == "Denmark" & party_pol_copartisan == "Co-partisan" & respondent_type == "Citizen"),
                                                         filter(C, country == "Denmark" & party_pol_copartisan == "Non co-partisan" & respondent_type == "Citizen"),
                                                         label1 = "Co-partisan", label2 = "Non co-partisan", group = "Respondent is a co-partisan\nof the politician",
                                                         formula = co_partisan_formula),
                                         country = "Denmark"),
                                  tibble(paired_conjoint(filter(C, country == "United States" & party_pol_copartisan == "Co-partisan" & respondent_type == "Politician"),
                                                         filter(C, country == "United States" & party_pol_copartisan == "Non co-partisan" & respondent_type == "Politician"),
                                                         label1 = "Co-partisan", label2 = "Non co-partisan", group = "Respondent is a co-partisan\nof the politician",
                                                         formula = co_partisan_formula),
                                         country = "US"))

G3_D_Politician_Country <- bind_rows(tibble(paired_conjoint(filter(C, country == "Belgium" & resp_exposure_binary == "Experienced social media harassment" & respondent_type == "Politician"),
                                                            filter(C, country == "Belgium" & resp_exposure_binary == "Has not experienced social media harassment" & respondent_type == "Politician"),
                                                            label1 = "Exp. w/ harassment", label2 = "No exp. w/ harassment", group = "Respondent experience with\nsocial media harassment"),
                                            country = "Belgium"),
                                     tibble(paired_conjoint(filter(C, country == "Chile" & resp_exposure_binary == "Experienced social media harassment" & respondent_type == "Politician"),
                                                            filter(C, country == "Chile" & resp_exposure_binary == "Has not experienced social media harassment" & respondent_type == "Politician"),
                                                            label1 = "Exp. w/ harassment", label2 = "No exp. w/ harassment", group = "Respondent experience with\nsocial media harassment"),
                                            country = "Chile"),
                                     tibble(paired_conjoint(filter(C, country == "Denmark" & resp_exposure_binary == "Experienced social media harassment" & respondent_type == "Politician"),
                                                            filter(C, country == "Denmark" & resp_exposure_binary == "Has not experienced social media harassment" & respondent_type == "Politician"),
                                                            label1 = "Exp. w/ harassment", label2 = "No exp. w/ harassment", group = "Respondent experience with\nsocial media harassment"),
                                            country = "Denmark"),
                                     tibble(paired_conjoint(filter(C, country == "United States" & resp_exposure_binary == "Experienced social media harassment" & respondent_type == "Politician"),
                                                            filter(C, country == "United States" & resp_exposure_binary == "Has not experienced social media harassment" & respondent_type == "Politician"),
                                                            label1 = "Exp. w/ harassment", label2 = "No exp. w/ harassment", group = "Respondent experience with\nsocial media harassment"),
                                            country = "US"))

G3_D_Citizen_Country <- bind_rows(tibble(paired_conjoint(filter(C, country == "Belgium" & resp_exposure_binary == "Experienced social media harassment" & respondent_type == "Citizen"),
                                                         filter(C, country == "Belgium" & resp_exposure_binary == "Has not experienced social media harassment" & respondent_type == "Citizen"),
                                                         label1 = "Exp. w/ harassment", label2 = "No exp. w/ harassment", group = "Respondent experience with\nsocial media harassment"),
                                            country = "Belgium"),
                                  tibble(paired_conjoint(filter(C, country == "Chile" & resp_exposure_binary == "Experienced social media harassment" & respondent_type == "Citizen"),
                                                         filter(C, country == "Chile" & resp_exposure_binary == "Has not experienced social media harassment" & respondent_type == "Citizen"),
                                                         label1 = "Exp. w/ harassment", label2 = "No exp. w/ harassment", group = "Respondent experience with\nsocial media harassment"),
                                         country = "Chile"),
                                  tibble(paired_conjoint(filter(C, country == "Denmark" & resp_exposure_binary == "Experienced social media harassment" & respondent_type == "Citizen"),
                                                         filter(C, country == "Denmark" & resp_exposure_binary == "Has not experienced social media harassment" & respondent_type == "Citizen"),
                                                         label1 = "Exp. w/ harassment", label2 = "No exp. w/ harassment", group = "Respondent experience with\nsocial media harassment"),
                                         country = "Denmark"),
                                  tibble(paired_conjoint(filter(C, country == "United States" & resp_exposure_binary == "Experienced social media harassment" & respondent_type == "Citizen"),
                                                         filter(C, country == "United States" & resp_exposure_binary == "Has not experienced social media harassment" & respondent_type == "Citizen"),
                                                         label1 = "Exp. w/ harassment", label2 = "No exp. w/ harassment", group = "Respondent experience with\nsocial media harassment"),
                                         country = "US"))



G3_Split_Country <- bind_rows(tibble(filter(G3_A_Politician_Country, feature == "Gender of politician"), respondent_sample = "Politician"),
                              tibble(filter(G3_A_Citizen_Country, feature == "Gender of politician"), respondent_sample = "Citizen"),
                              tibble(filter(G3_B_Politician_Country, feature == "Gender of politician"), respondent_sample = "Politician"),
                              tibble(filter(G3_B_Citizen_Country, feature == "Gender of politician"), respondent_sample = "Citizen"),
                              tibble(filter(G3_C_Politician_Country, feature == "Gender of politician"), respondent_sample = "Politician"),
                              tibble(filter(G3_C_Citizen_Country, feature == "Gender of politician"), respondent_sample = "Citizen"),
                              tibble(filter(G3_D_Politician_Country, feature == "Gender of politician"), respondent_sample = "Politician"),
                              tibble(filter(G3_D_Citizen_Country, feature == "Gender of politician"), respondent_sample = "Citizen"))
G3_Split_Country <- filter(G3_Split_Country, !is.na(std.error))

G3_Split_Country$group <- factor(G3_Split_Country$group, levels = c("Respondent gender",
                                                                    "Respondent ideology",
                                                                    "Respondent is a co-partisan\nof the politician",
                                                                    "Respondent experience with\nsocial media harassment"))
G3_Split_Country$respondent_type <- factor(G3_Split_Country$respondent_type,
                                           levels = c("Woman", "Man",
                                                      "Right-wing", "Left-wing",
                                                      "Non co-partisan", "Co-partisan",
                                                      "No exp. w/ harassment", "Exp. w/ harassment"))


G3A_Split_Country <- ggplot(filter(G3_Split_Country, respondent_sample == "Citizen"),
       aes(x = estimate, y = respondent_type, yend = respondent_type,
           color = group)) +
     my.theme(base_size = 7, borderless = 2,
              remove.ticks.y = TRUE, remove.ticks.x = TRUE,
              grid.y_colour = "grey65", grid.y_linetype = 3,
              grid.x_colour = "grey85", grid.x_linetype = 1) +
     labs(x = "AMCE (in percentage points)\nfor whether the politician is a woman",
          y = "") +
     facet_wrap(country ~ group, scale = "free") +
     scale_x_continuous(breaks = seq(-0.03, 0.30, by = 0.03),
                        labels = seq(-3, 30, by = 3)) +
     geom_vline(xintercept = 0, linetype = 1, size = 0.35, color = "grey50") +
     geom_point(size = 0, color = "transparent") +
     geom_point(data = filter(G3_Split_Country, respondent_sample == "Citizen"),
                size = 2.5) +
     geom_segment(data = filter(G3_Split_Country, respondent_sample == "Citizen"), aes(x = lower, xend = upper), size = 0.4) +
     scale_colour_manual(values = c("#5200FF", "#FF3E00", "#00bd8e", "#A1B811")) +
     theme(legend.position = "none",
           strip.text.x = element_text(face = "plain", angle = 0, hjust = 0, size = 7))

G3B_Split_Country <- ggplot(filter(G3_Split_Country, respondent_sample == "Politician"),
       aes(x = estimate, y = respondent_type, yend = respondent_type,
           color = group)) +
     my.theme(base_size = 7, borderless = 2,
              remove.ticks.y = TRUE, remove.ticks.x = TRUE,
              grid.y_colour = "grey65", grid.y_linetype = 3,
              grid.x_colour = "grey85", grid.x_linetype = 1) +
     labs(x = "AMCE (in percentage points)\nfor whether the politician is a woman",
          y = "") +
     facet_wrap(country ~ group, scale = "free") +
     scale_x_continuous(breaks = seq(-0.03, 0.30, by = 0.03),
                        labels = seq(-3, 30, by = 3)) +
     geom_vline(xintercept = 0, linetype = 1, size = 0.35, color = "grey50") +
     geom_point(size = 0, color = "transparent") +
     geom_point(data = filter(G3_Split_Country, respondent_sample == "Politician"),
                size = 2.5) +
     geom_segment(data = filter(G3_Split_Country, respondent_sample == "Politician"), aes(x = lower, xend = upper), size = 0.4) +
     scale_colour_manual(values = c("#5200FF", "#FF3E00", "#00bd8e", "#A1B811")) +
     theme(legend.position = "none",
           strip.text.x = element_text(face = "plain", angle = 0, hjust = 0, size = 7))

# FIGURE E4
pdf("Figures/Figure_5_Country_Citizen-RAW.pdf", 10, 5.5)
G3A_Split_Country
dev.off()

# FIGURE E5
pdf("Figures/Figure_5_Country_Politician-RAW.pdf", 10, 5.5)
G3B_Split_Country
dev.off()






# APPENDIX H: Triple interactions
# Hypotheses 7, 8, and 9 from the pre-registration
formula_women_pol_man_user_resp_woman <- chose_profile ~ text_pol + text_user +
                                                         party_pol_copartisan + woman_user +
                                                         woman_pol + gendered + resp_gender +
                                                         woman_pol_man_user +
                                                         woman_pol_resp_woman +
                                                         man_user_resp_woman +
                                                         woman_pol_man_user_resp_woman

formula_women_pol_man_user_resp_left_wing <- chose_profile ~ text_pol + text_user +
                                                             party_pol_copartisan + woman_user +
                                                             woman_pol + gendered + resp_left_wing +
                                                             woman_pol_man_user +
                                                             woman_pol_resp_left_wing +
                                                             man_user_resp_left_wing +
                                                             woman_pol_man_user_resp_left_wing

formula_women_pol_man_user_gendered <- chose_profile ~ text_pol + text_user +
                                                       party_pol_copartisan + woman_user +
                                                       woman_pol + gendered +
                                                       woman_pol_man_user +
                                                       woman_pol_gendered +
                                                       man_user_gendered +
                                                       woman_pol_man_user_gendered

formula_women_pol_gendered_resp_woman <- chose_profile ~ text_pol + text_user +
                                                         party_pol_copartisan + woman_user +
                                                         woman_pol + gendered + resp_gender +
                                                         woman_pol_resp_woman +
                                                         woman_pol_gendered +
                                                         gendered_resp_woman +
                                                         woman_pol_gendered_resp_woman

G4_A <- paired_conjoint(filter(C, respondent_type == "Citizen"),
                        filter(C, respondent_type == "Politician"),
                        label1 = "Citizen sample", label2 = "Politician sample",
                        group = "Woman politician & Man user & Woman respondent",
                        formula = formula_women_pol_man_user_resp_woman)

G4_B <- paired_conjoint(filter(C, respondent_type == "Citizen"),
                        filter(C, respondent_type == "Politician"),
                        label1 = "Citizen sample", label2 = "Politician sample",
                        group = "Woman politician & Man user & Left-wing respondent",
                        formula = formula_women_pol_man_user_resp_left_wing)

G4_C <- paired_conjoint(filter(C, respondent_type == "Citizen"),
                        filter(C, respondent_type == "Politician"),
                        label1 = "Citizen sample", label2 = "Politician sample",
                        group = "Woman politician & Man user & Gendered text",
                        formula = formula_women_pol_man_user_gendered)

G4_D <- paired_conjoint(filter(C, respondent_type == "Citizen"),
                        filter(C, respondent_type == "Politician"),
                        label1 = "Citizen sample", label2 = "Politician sample",
                        group = "Woman politician & Gendered text & Woman respondent",
                        formula = formula_women_pol_gendered_resp_woman)

G4 <- bind_rows(G4_A, G4_B, G4_C, G4_D)

# Interaction results
data.frame(na.omit(G4[G4$feature == "Triple interaction", ]) %>%
           select(respondent_type, level, estimate, p.value) %>%
           mutate(level = str_replace_all(level, "\n", " "),
                  estimate = round(estimate, 3),
                  p = round(p.value, 3)))


G4$facet <- paste0("Interaction of interest: ", str_replace_all(G4$level, "\n", " "))

G4 <- G4 %>%
      mutate(facet = recode(facet,
                            "Interaction of interest: Woman politician x man user x woman respondent" = "(1) Interaction of interest: Woman politician x man user x woman respondent",
                            "Interaction of interest: Woman politician x man user x left-wing" = "(2) Interaction of interest: Woman politician x man user x left-wing",
                            "Interaction of interest: Woman politician x man user x gendered text" = "(3) Interaction of interest: Woman politician x man user x gendered text",
                            "Interaction of interest: Woman politician x gendered text x woman respondent" = "(4) Interaction of interest: Woman politician x gendered text x woman respondent"),
             facet = factor(facet, levels = c("(1) Interaction of interest: Woman politician x man user x woman respondent",
                                              "(2) Interaction of interest: Woman politician x man user x left-wing",
                                              "(3) Interaction of interest: Woman politician x man user x gendered text",
                                              "(4) Interaction of interest: Woman politician x gendered text x woman respondent")))


# FIGURE I10
pdf("Figures/Triple_Interactions-RAW.pdf", 5.5, 4.5)
ggplot(filter(G4, feature %in% c("Triple interaction")),
       aes(x = estimate, y = level, yend = level,
           color = level, shape = respondent_type)) +
     my.theme(base_size = 7, borderless = 2,
              remove.ticks.y = TRUE, remove.ticks.x = TRUE,
              grid.y_colour = "grey65", grid.y_linetype = 3,
              grid.x_colour = "grey85", grid.x_linetype = 1) +
     labs(x = "AMCE (in percentage points)", y = "") +
     ggforce::facet_col( ~ facet, scales = "free_y", space = "fixed") +
     coord_cartesian(xlim = c(-0.10, 0.12)) +
     scale_x_continuous(breaks = seq(-0.10, 0.12, by = 0.02),
                        labels = c("-10", "-8", "-6", "-4", "-2", "0", "2", "4", "6", "8", "10", "12")) +
     geom_vline(xintercept = 0, linetype = 1, size = 0.35, color = "grey50") +
     geom_segment(data = filter(G4, feature %in% c("Triple interaction") & !is.na(std.error) & respondent_type == "Citizen sample"),
                  aes(x = lower, xend = upper),
                  size = 0.4, position = position_nudge(x = 0, y = 0.2)) +
     geom_point(data = filter(G4, feature %in% c("Triple interaction") & !is.na(std.error) & respondent_type == "Citizen sample"),
                size = 2.75, position = position_nudge(x = 0, y = 0.2),
                fill = "white", stroke = 1) +
     geom_segment(data = filter(G4, feature %in% c("Triple interaction") & !is.na(std.error) & respondent_type == "Politician sample"),
                  aes(x = lower, xend = upper),
                  size = 0.4, position = position_nudge(x = 0, y = -0.2)) +
     geom_point(data = filter(G4, feature %in% c("Triple interaction") & !is.na(std.error) & respondent_type == "Politician sample"),
                size = 2.75, position = position_nudge(x = 0, y = -0.2),
                fill = "white", stroke = 1) +
     geom_point(data = filter(G4, feature %in% c("Triple interaction") & is.na(std.error) & respondent_type == "Politician sample"),
                size = 2, position = position_nudge(x = 0, y = 0),
                color = "black", fill = "red") +
     geom_label(data = filter(G4, feature == "Triple interaction" & level == "Woman politician\nx man user\nx woman respondent"),
          aes(x = upper, y = level, label = respondent_type),
          hjust = -0.1, vjust = 0.3, fontface = "bold",
          label.padding = unit(0.1, "lines"), label.size = NA,
               label.r = unit(0.15, "lines"),
          position = position_dodge(width = -0.8), size = 2.5) +
     scale_color_manual(values = c("#5200FF", "#00bd8e", "#FF3E00", "#A1B811")) +
     scale_shape_manual(values = c(16, 21)) +
     theme(legend.position = "none",
           strip.text.x = element_text(angle = 0, hjust = 0, size = 7))
dev.off()


# APPENDIX J: Does the effect of a woman politician depend on what conjoint
# or vignette task?

task_number_interaction_formula <- chose_profile ~ text_pol +
                                                   text_user +
                                                   party_pol_copartisan +
                                                   woman_user +
                                                   woman_pol +
                                                   gendered +
                                                   task_number +
                                                   woman_pol_task_number

Task_Conjoint <- paired_conjoint(filter(C, respondent_type == "Citizen"),
                                 filter(C, respondent_type == "Politician"),
                                 label1 = "Citizen sample", label2 = "Politician sample",
                                 group = "Gender of politician & task number",
                                 formula = task_number_interaction_formula)

Task_Conjoint$feature <- as.character(Task_Conjoint$feature)
Task_Conjoint$feature[Task_Conjoint$level %in% c("Woman politician x Task 1",
                                                 "Woman politician x Task 2",
                                                 "Woman politician x Task 3",
                                                 "Woman politician x Task 4",
                                                 "Woman politician x Task 5")] <- "Task interaction"

Task_Conjoint$respondent_type <- factor(Task_Conjoint$respondent_type,
                                        levels = c("Politician sample",
                                                   "Citizen sample"))

Task_Conjoint$level <- factor(Task_Conjoint$level,
                              levels =  c("Co-partisan",
                                          "Man user",
                                          "Gendered text",
                                          "Woman politician x Task 5",
                                          "Woman politician x Task 4",
                                          "Woman politician x Task 3",
                                          "Woman politician x Task 2",
                                          "Woman politician x Task 1",
                                          "Woman politician"))

Task_Conjoint$task_binary <- ifelse(Task_Conjoint$level %in%
                                    c("Woman politician x Task 1",
                                      "Woman politician x Task 2",
                                      "Woman politician x Task 3",
                                      "Woman politician x Task 4",
                                      "Woman politician x Task 5"),
                                      "Task interaction", "Other")

G_Task_Conjoint <- filter(Task_Conjoint, (feature != "Reply from user" | grepl("task", level)) & !is.na(std.error)) %>%
                   ggplot(aes(x = estimate, y = level, yend = level,
                              color = feature, fill = feature)) +
                        my.theme(base_size = 8,
                                 borderless = 2, remove.ticks.y = TRUE, remove.ticks.x = TRUE,
                                 grid.y_colour = "grey65", grid.y_linetype = 3,
                                 grid.x_colour = "grey85", grid.x_linetype = 1) +
                        labs(x = "AMCE (in percentage points)", y = "") +
                        facet_wrap( ~ respondent_type, scales = "fixed") +
                        coord_cartesian(xlim = c(-0.07, 0.1)) +
                        scale_x_continuous(breaks = seq(-0.08, 0.1, by = 0.02),
                                           labels = c("-8", "-6", "-4", "-2", "0", "2", "4", "6", "8", "10")) +
                        geom_vline(xintercept = 0, linetype = 1, size = 0.35, color = "grey50") +
                        geom_point(size = 0, color = "transparent") +
                        geom_segment(aes(x = lower, xend = upper), size = 0.5) +
                        geom_point(size = 2.25, stroke = 1, shape = 21) +
                        scale_color_manual(values = c("grey40", "#5200FF", "grey40", "grey40", "#5200FF")) +
                        scale_fill_manual(values = c("grey40", "#5200FF", "grey40", "grey40", "white")) +
                        theme(legend.position = "none",
                              strip.text.x = element_text(color = "transparent", angle = 0, hjust = 0.5, size = 8))

# FIGURE J11
pdf("Figures/Task_Number_Conjoint-RAW.pdf", 7.5, 2.75)
ggdraw() +
  draw_plot(G_Task_Conjoint, x = 0, y = 0, width = 1, height = 0.98) +
  draw_plot_label(label = c("A.", "B."),
                x = c(0.21, 0.61), y = c(0.992, 0.992),
                size = 8, hjust = 0, vjust = 1) +
  draw_plot_label(label = c("Politician sample",
                            "Citizen sample"),
                  x = c(0.23, 0.63), y = c(0.992, 0.992),
                  fontface = "bold",
                  size = 8, hjust = 0, vjust = 1)
dev.off()



# Are national and local politicians different in their conjoint responses?
# Confine to Denmark & Chile where we have both types
# This is severely underpowered, but is for a response to a reviewer.
# NOTE: The variable indicating whether a politician is a local-level or
# national-level politician is not included in the dataset to protect anonymity
# These regressions and the corresponding figure thus cannot be run with the
# replication data.
model_national_local <- paired_conjoint(filter(C, respondent_type_three == "National politician" & country %in% c("Chile", "Denmark")),
                                        filter(C, respondent_type_three == "Local politician" & country %in% c("Chile", "Denmark")),
                                        label1 = "National politician",
                                        label2 = "Local politician",
                                        group = "National & local politicians")

# Difference in conjoint estimates for a woman politician among local and
# national politicians
# Estimates are 0.0788 for national & 0.0868 for local
model_national_local %>%
select(respondent_type, feature, level, estimate, lower, upper) %>%
filter(!feature %in% c("Reply from user") & !is.na(feature)) %>%
arrange(feature, level)

model_national_local <- model_national_local %>%
                        mutate(level = factor(level, levels = c("Man politician", "Woman politician",
                                                                "Non-gendered text", "Gendered text",
                                                                "Woman user", "Man user",
                                                                 "Non co-partisan", "Co-partisan")),
                               respondent_type = factor(respondent_type,
                                                        levels = c("National politician",
                                                                   "Local politician")))

# FIGURE K13
# NOTE: Again, this figure cannot be run because it requires the variable
# distinguishing local from national politicians, which is excluded from the
# replication data to protect anonymity
pdf("Figures/Main_Result_National_Local-RAW.pdf", 6.5, 4)
ggplot(filter(model_national_local, feature != "Reply from user"),
       aes(x = estimate, y = level, yend = level,
           color = feature, fill = feature, shape = respondent_type)) +
     my.theme(base_size = 8,
              borderless = 2, remove.ticks.y = TRUE, remove.ticks.x = TRUE,
              grid.y_colour = "grey65", grid.y_linetype = 3,
              grid.x_colour = "grey85", grid.x_linetype = 1) +
     labs(x = "AMCE (in percentage points)", y = "") +
     ggforce::facet_col( ~ feature, scales = "free_y", space = "free") +
     coord_cartesian(xlim = c(-0.16, 0.20)) +
     scale_x_continuous(breaks = seq(-0.16, 0.20, by = 0.02),
                        labels = c("-16", "-14", "-12", "-10", "-8", "-6", "-4", "-2", "0",
                                   "2", "4", "6", "8", "10", "12", "14", "16", "18",
                                   "20")) +
     geom_vline(xintercept = 0, linetype = 1, size = 0.35, color = "grey50") +
     geom_point(data = filter(model_national_local, feature != "Reply from user"),
                size = 0, color = "transparent", fill = "transparent") +
     geom_segment(data = filter(model_national_local, feature != "Reply from user" & !is.na(std.error) & respondent_type == "Local politician"),
                  aes(x = lower, xend = upper),
                  size = 0.5, position = position_nudge(x = 0, y = 0.25)) +
     geom_point(data = filter(model_national_local, feature != "Reply from user" & !is.na(std.error) & respondent_type == "Local politician"),
                size = 2.5, position = position_nudge(x = 0, y = 0.25), stroke = 1) +
     geom_segment(data = filter(model_national_local, feature != "Reply from user" & !is.na(std.error) & respondent_type == "National politician"),
                  aes(x = lower, xend = upper), 
                  size = 0.5, position = position_nudge(x = 0, y = -0.25)) +
     geom_point(data = filter(model_national_local, feature != "Reply from user" & !is.na(std.error) & respondent_type == "National politician"),
                size = 2.5, position = position_nudge(x = 0, y = -0.25),
                fill = "white", stroke = 1) +
     geom_label(data = filter(model_national_local, feature == "Gender of politician"),
               aes(x = lower, y = level, label = respondent_type),
               hjust = 1.05, vjust = 0.3, fontface = "bold",
               label.padding = unit(0.1, "lines"), label.size = NA,
               label.r = unit(0.15, "lines"), fill = "white",
               position = position_dodge(width = 1), size = 2.75) +
     geom_point(data = filter(model_national_local, feature != "Reply from user" & is.na(std.error) & respondent_type == "National politician"),
                size = 2, position = position_nudge(x = 0, y = 0),
                color = "black", fill = "black") +
     scale_color_manual(values = c("#5200FF", "#00bd8e", "#FF3E00", "#A1B811")) +
     scale_fill_manual(values = c("#5200FF", "#00bd8e", "#FF3E00", "#A1B811")) +
     scale_shape_manual(values = c(21, 21)) +
     theme(legend.position = "none",
           strip.text.x = element_text(angle = 0, hjust = 0, size = 8, face = "bold"))
dev.off()





# Do the various issues talked ahout by the fictitious politicians have
# different effects by country?

text_pol_group_by_country <- bind_rows(paired_conjoint(filter(C, respondent_type == "Citizen" & !is.na(chose_profile) & country == "Belgium"),
                          filter(C, respondent_type == "Politician" & !is.na(chose_profile) & country == "Belgium"),
                          label1 = "Citizen sample", label2 = "Politician sample",
                          group = "Belgium",
                          formula = chose_profile ~ text_pol_group +
                                                    text_user +
                                                    party_pol_copartisan +
                                                    woman_user +
                                                    woman_pol +
                                                    gendered),
          paired_conjoint(filter(C, respondent_type == "Citizen" & !is.na(chose_profile) & country == "Chile"),
                          filter(C, respondent_type == "Politician" & !is.na(chose_profile) & country == "Chile"),
                          label1 = "Citizen sample", label2 = "Politician sample",
                          group = "Chile",
                          formula = chose_profile ~ text_pol_group +
                                                    text_user +
                                                    party_pol_copartisan +
                                                    woman_user +
                                                    woman_pol +
                                                    gendered),
          paired_conjoint(filter(C, respondent_type == "Citizen" & !is.na(chose_profile) & country == "Denmark"),
                          filter(C, respondent_type == "Politician" & !is.na(chose_profile) & country == "Denmark"),
                          label1 = "Citizen sample", label2 = "Politician sample",
                          group = "Denmark",
                          formula = chose_profile ~ text_pol_group +
                                                    text_user +
                                                    party_pol_copartisan +
                                                    woman_user +
                                                    woman_pol +
                                                    gendered),
          paired_conjoint(filter(C, respondent_type == "Citizen" & !is.na(chose_profile) & country == "United States"),
                          filter(C, respondent_type == "Politician" & !is.na(chose_profile) & country == "United States"),
                          label1 = "Citizen sample", label2 = "Politician sample",
                          group = "US",
                          formula = chose_profile ~ text_pol_group +
                                                    text_user +
                                                    party_pol_copartisan +
                                                    woman_user +
                                                    woman_pol +
                                                    gendered))

text_pol_group_by_country$group <- factor(text_pol_group_by_country$group, levels = c("Belgium", "Chile", "Pooled estimate", "Denmark", "US"))

plot_features <- c("Content of politician's post")

G_Pol_Group_Politicians <- ggplot(filter(text_pol_group_by_country, feature %in% plot_features),
       aes(x = estimate, xend = estimate, y = level, yend = level,
           color = group, size = group,
           shape = group, fill = group,
           alpha = group)) +
     my.theme(base_size = 7, borderless = 2, remove.ticks.y = TRUE, remove.ticks.x = TRUE,
              grid.y_colour = "grey65", grid.y_linetype = 3,
              grid.x_colour = "grey85", grid.x_linetype = 1) +
     labs(x = "AMCE (in percentage points)", y = "") +
     ggforce::facet_col( ~ feature, scales = "free_y", space = "free") +
     coord_cartesian(xlim = c(-0.09, 0.09)) +
     scale_x_continuous(breaks = seq(-0.06, 0.12, by = 0.02),
                        labels = c("-6", "-4", "-2", "0", "2", "4", "6", "8", "10", "12")) +
     geom_vline(xintercept = 0, linetype = 1, size = 0.35, color = "grey50") +
     geom_point(size = 0, color = "transparent") +
     geom_linerange(data = filter(text_pol_group_by_country, feature %in% plot_features & !is.na(std.error) & respondent_type == "Politician sample"),
                    aes(xmin = lower, xmax = upper, alpha = group),
                    position = position_dodge2(width = 0.5),
                    size = 0.5, show.legend = FALSE) +
     geom_point(data = filter(text_pol_group_by_country, feature %in% plot_features & !is.na(std.error) & respondent_type == "Politician sample"),
                position = position_dodge2(width = 0.5), alpha = 1) +
     geom_point(data = filter(text_pol_group_by_country, group == "Belgium", feature %in% plot_features & is.na(std.error) & respondent_type == "Politician sample"),
                size = 1.5, shape = 16, alpha = 1, color = "black", fill = "black") +
     scale_size_manual(values = c(2, 2, 2, 2)) +
     scale_shape_manual(values = c(16, 16, 16, 16)) +
     scale_fill_manual(values = c("#00bd8e", "#FF3E00", "#A1B811", "#5200FF")) +
     scale_colour_manual(values = c("#00bd8e", "#FF3E00", "#A1B811", "#5200FF")) +
     scale_alpha_manual(values = c(1, 1, 1, 1, 1)) +
     theme(legend.position = "top",
           strip.text.x = element_text(angle = 0, hjust = 0, size = 7, face = "bold")) +
     guides(size = guide_legend(override.aes = list(size = 2, alpha = 1)))


G_Pol_Group_Citizens <- ggplot(filter(text_pol_group_by_country, feature %in% plot_features),
       aes(x = estimate, xend = estimate, y = level, yend = level,
           color = group, size = group,
           shape = group, fill = group,
           alpha = group)) +
     my.theme(base_size = 7, borderless = 2, remove.ticks.y = TRUE, remove.ticks.x = TRUE,
              grid.y_colour = "grey65", grid.y_linetype = 3,
              grid.x_colour = "grey85", grid.x_linetype = 1) +
     labs(x = "AMCE (in percentage points)", y = "") +
     ggforce::facet_col( ~ feature, scales = "free_y", space = "free") +
     coord_cartesian(xlim = c(-0.09, 0.09)) +
     scale_x_continuous(breaks = seq(-0.06, 0.12, by = 0.02),
                        labels = c("-6", "-4", "-2", "0", "2", "4", "6", "8", "10", "12")) +
     geom_vline(xintercept = 0, linetype = 1, size = 0.35, color = "grey50") +
     geom_point(size = 0, color = "transparent") +
     geom_linerange(data = filter(text_pol_group_by_country, feature %in% plot_features & !is.na(std.error) & respondent_type == "Citizen sample"),
                    aes(xmin = lower, xmax = upper, alpha = group),
                    position = position_dodge2(width = 0.5),
                    size = 0.5, show.legend = FALSE) +
     geom_point(data = filter(text_pol_group_by_country, feature %in% plot_features & !is.na(std.error) & respondent_type == "Citizen sample"),
                position = position_dodge2(width = 0.5), alpha = 1) +
     geom_point(data = filter(text_pol_group_by_country, group == "Belgium", feature %in% plot_features & is.na(std.error) & respondent_type == "Citizen sample"),
                size = 1.5, shape = 16, alpha = 1, color = "black", fill = "black") +
     scale_size_manual(values = c(2, 2, 2, 2)) +
     scale_shape_manual(values = c(16, 16, 16, 16)) +
     scale_fill_manual(values = c("#00bd8e", "#FF3E00", "#A1B811", "#5200FF")) +
     scale_colour_manual(values = c("#00bd8e", "#FF3E00", "#A1B811", "#5200FF")) +
     scale_alpha_manual(values = c(1, 1, 1, 1, 1)) +
     theme(legend.position = "top",
           strip.text.x = element_text(angle = 0, hjust = 0, size = 7, face = "bold")) +
     guides(size = guide_legend(override.aes = list(size = 2, alpha = 1)))


# FIGURE M15
pdf("Figures/Pol_Group_By_Country-RAW.pdf", 6.5, 3.75)
ggdraw() +
  draw_plot(G_Pol_Group_Politicians, x = -0.02, y = 0.01, width = 0.51, height = 1) +
  draw_plot(G_Pol_Group_Citizens, x = 0.48, y = 0.01, width = 0.51, height = 1) +
  draw_plot_label(label = c("A.",
                            "B."),
                  x = c(0.01, 0.51), y = c(0.992, 0.992),
                  size = 8, hjust = 0, vjust = 1) +
  draw_plot_label(label = c("Politician sample",
                            "Citizen sample"),
                  x = c(0.035, 0.535), y = c(0.992, 0.992),
                  fontface = "plain",
                  size = 8, hjust = 0, vjust = 1)
dev.off()




# Does the effect of a politician's gender depend on the issue that a politician
# is writing about on social media (by country)
gender_effect_text_pol_group_by_country <- bind_rows(paired_conjoint(filter(C, respondent_type == "Citizen" & !is.na(chose_profile) & country == "Belgium"),
                          filter(C, respondent_type == "Politician" & !is.na(chose_profile) & country == "Belgium"),
                          label1 = "Citizen sample", label2 = "Politician sample",
                          group = "Belgium",
                          formula = chose_profile ~ text_pol_group +
                                                    text_user +
                                                    party_pol_copartisan +
                                                    woman_user +
                                                    woman_pol +
                                                    gendered +
                                                    woman_pol_text_pol_group),
                                      paired_conjoint(filter(C, respondent_type == "Citizen" & !is.na(chose_profile) & country == "Chile"),
                                                      filter(C, respondent_type == "Politician" & !is.na(chose_profile) & country == "Chile"),
                                                      label1 = "Citizen sample", label2 = "Politician sample",
                                                      group = "Chile",
                                                      formula = chose_profile ~ text_pol_group +
                                                                                text_user +
                                                                                party_pol_copartisan +
                                                                                woman_user +
                                                                                woman_pol +
                                                                                gendered +
                                                                                woman_pol_text_pol_group),
                                      paired_conjoint(filter(C, respondent_type == "Citizen" & !is.na(chose_profile) & country == "Denmark"),
                                                      filter(C, respondent_type == "Politician" & !is.na(chose_profile) & country == "Denmark"),
                                                      label1 = "Citizen sample", label2 = "Politician sample",
                                                      group = "Denmark",
                                                      formula = chose_profile ~ text_pol_group +
                                                                                text_user +
                                                                                party_pol_copartisan +
                                                                                woman_user +
                                                                                woman_pol +
                                                                                gendered +
                                                                                woman_pol_text_pol_group),
                                      paired_conjoint(filter(C, respondent_type == "Citizen" & !is.na(chose_profile) & country == "United States"),
                                                      filter(C, respondent_type == "Politician" & !is.na(chose_profile) & country == "United States"),
                                                      label1 = "Citizen sample", label2 = "Politician sample",
                                                      group = "US",
                                                      formula = chose_profile ~ text_pol_group +
                                                                                text_user +
                                                                                party_pol_copartisan +
                                                                                woman_user +
                                                                                woman_pol +
                                                                                gendered +
                                                                                woman_pol_text_pol_group))


gender_effect_text_pol_group_by_country$group <- factor(gender_effect_text_pol_group_by_country$group,
                                                        levels = c("Belgium", "Chile", "Denmark", "US"))

gender_effect_text_pol_group_by_country$level <- as.character(gender_effect_text_pol_group_by_country$level)
gender_effect_text_pol_group_by_country$level <- str_replace_all(gender_effect_text_pol_group_by_country$level, " x ", "\nx ")

plot_features <- c("Interaction")

G_Pol_Group_Politicians_Interaction <- ggplot(filter(gender_effect_text_pol_group_by_country, feature %in% plot_features),
       aes(x = estimate, xend = estimate, y = level, yend = level,
           color = group, size = group,
           shape = group, fill = group,
           alpha = group)) +
     my.theme(base_size = 7, borderless = 2, remove.ticks.y = TRUE, remove.ticks.x = TRUE,
              grid.y_colour = "grey65", grid.y_linetype = 3,
              grid.x_colour = "grey85", grid.x_linetype = 1) +
     labs(x = "AMCE (in percentage points)", y = "") +
     ggforce::facet_col( ~ feature, scales = "free_y", space = "free") +
     coord_cartesian(xlim = c(-0.2, 0.2)) +
     scale_x_continuous(breaks = seq(-0.2, 0.2, by = 0.04),
                        labels = seq(-20, 20, by = 4)) +
     geom_vline(xintercept = 0, linetype = 1, size = 0.35, color = "grey50") +
     geom_point(size = 0, color = "transparent") +
     geom_linerange(data = filter(gender_effect_text_pol_group_by_country, feature %in% plot_features & !is.na(std.error) & respondent_type == "Politician sample"),
                    aes(xmin = lower, xmax = upper, alpha = group),
                    position = position_dodge2(width = 0.5),
                    size = 0.5, show.legend = FALSE) +
     geom_point(data = filter(gender_effect_text_pol_group_by_country, feature %in% plot_features & !is.na(std.error) & respondent_type == "Politician sample"),
                position = position_dodge2(width = 0.5), alpha = 1) +
     geom_point(data = filter(gender_effect_text_pol_group_by_country, group == "Belgium", feature %in% plot_features & is.na(std.error) & respondent_type == "Politician sample"),
                size = 1.5, shape = 16, alpha = 1, color = "black", fill = "black") +
     scale_size_manual(values = c(2, 2, 2, 2)) +
     scale_shape_manual(values = c(16, 16, 16, 16)) +
     scale_fill_manual(values = c("#00bd8e", "#FF3E00", "#A1B811", "#5200FF")) +
     scale_colour_manual(values = c("#00bd8e", "#FF3E00", "#A1B811", "#5200FF")) +
     scale_alpha_manual(values = c(1, 1, 1, 1, 1)) +
     theme(legend.position = "top",
           strip.text.x = element_text(angle = 0, hjust = 0, size = 7, face = "bold")) +
     guides(size = guide_legend(override.aes = list(size = 2, alpha = 1)))


G_Pol_Group_Citizens_Interacation <- ggplot(filter(gender_effect_text_pol_group_by_country, feature %in% plot_features),
       aes(x = estimate, xend = estimate, y = level, yend = level,
           color = group, size = group,
           shape = group, fill = group,
           alpha = group)) +
     my.theme(base_size = 7, borderless = 2, remove.ticks.y = TRUE, remove.ticks.x = TRUE,
              grid.y_colour = "grey65", grid.y_linetype = 3,
              grid.x_colour = "grey85", grid.x_linetype = 1) +
     labs(x = "AMCE (in percentage points)", y = "") +
     ggforce::facet_col( ~ feature, scales = "free_y", space = "free") +
     coord_cartesian(xlim = c(-0.2, 0.2)) +
     scale_x_continuous(breaks = seq(-0.2, 0.2, by = 0.04),
                        labels = seq(-20, 20, by = 4)) +
     geom_vline(xintercept = 0, linetype = 1, size = 0.35, color = "grey50") +
     geom_point(size = 0, color = "transparent") +
     geom_linerange(data = filter(gender_effect_text_pol_group_by_country, feature %in% plot_features & !is.na(std.error) & respondent_type == "Citizen sample"),
                    aes(xmin = lower, xmax = upper, alpha = group),
                    position = position_dodge2(width = 0.5),
                    size = 0.5, show.legend = FALSE) +
     geom_point(data = filter(gender_effect_text_pol_group_by_country, feature %in% plot_features & !is.na(std.error) & respondent_type == "Citizen sample"),
                position = position_dodge2(width = 0.5), alpha = 1) +
     geom_point(data = filter(gender_effect_text_pol_group_by_country, group == "Belgium", feature %in% plot_features & is.na(std.error) & respondent_type == "Citizen sample"),
                size = 1.5, shape = 16, alpha = 1, color = "black", fill = "black") +
     scale_size_manual(values = c(2, 2, 2, 2)) +
     scale_shape_manual(values = c(16, 16, 16, 16)) +
     scale_fill_manual(values = c("#00bd8e", "#FF3E00", "#A1B811", "#5200FF")) +
     scale_colour_manual(values = c("#00bd8e", "#FF3E00", "#A1B811", "#5200FF")) +
     scale_alpha_manual(values = c(1, 1, 1, 1, 1)) +
     theme(legend.position = "top",
           strip.text.x = element_text(angle = 0, hjust = 0, size = 7, face = "bold")) +
     guides(size = guide_legend(override.aes = list(size = 2, alpha = 1)))

# FIGURE M16
pdf("Figures/Interaction_Pol_Group_By_Country-RAW.pdf", 7, 3.5)
ggdraw() +
  draw_plot(G_Pol_Group_Politicians_Interaction, x = -0.02, y = 0.01, width = 0.51, height = 1) +
  draw_plot(G_Pol_Group_Citizens_Interacation, x = 0.48, y = 0.01, width = 0.51, height = 1) +
  draw_plot_label(label = c("A.",
                            "B."),
                  x = c(0.01, 0.51), y = c(0.992, 0.992),
                  size = 8, hjust = 0, vjust = 1) +
  draw_plot_label(label = c("Politician sample",
                            "Citizen sample"),
                  x = c(0.035, 0.535), y = c(0.992, 0.992),
                  fontface = "plain",
                  size = 8, hjust = 0, vjust = 1)
dev.off()




# Is there variation by whether the politician is a person of color?
poc_formula <- chose_profile ~ text_pol + text_user +
                               party_pol_copartisan + woman_user +
                               woman_pol + poc_pol + gendered

POC_model <- paired_conjoint(filter(C, respondent_type == "Citizen"),
                        filter(C, respondent_type == "Politician"),
                        label1 = "Citizen sample", label2 = "Politician sample",
                        group = "POC",
                        formula = poc_formula)

# p-values for politican of color (implicit) treatment
# pol sample: p < 0.01; cit sample: p = 0.80
POC_model %>%
filter(feature == "Politician is a Person of Color") %>%
select(feature, level, estimate, p.value)


# FIGURE L14
pdf("Figures/Main_Result_POC-RAW.pdf", 5, 5.1)
ggplot(filter(POC_model, feature != "Reply from user"),
       aes(x = estimate, y = level, yend = level, color = feature, shape = respondent_type)) +
     my.theme(base_size = 8,
              borderless = 2, remove.ticks.y = TRUE, remove.ticks.x = TRUE,
              grid.y_colour = "grey65", grid.y_linetype = 3,
              grid.x_colour = "grey85", grid.x_linetype = 1) +
     labs(x = "AMCE (in percentage points)", y = "") +
     ggforce::facet_col( ~ feature, scales = "free_y", space = "free") +
     coord_cartesian(xlim = c(-0.02, 0.09)) +
     scale_x_continuous(breaks = seq(-0.06, 0.10, by = 0.02),
                        labels = c("-6", "-4", "-2", "0", "2", "4", "6", "8", "10")) +
     geom_vline(xintercept = 0, linetype = 1, size = 0.35, color = "grey50") +
     geom_point(size = 0, color = "transparent") +
     geom_segment(data = filter(POC_model, feature != "Reply from user" & !is.na(std.error) & respondent_type == "Citizen sample"),
                  aes(x = lower, xend = upper),
                  size = 0.5, position = position_nudge(x = 0, y = 0.25)) +
     geom_point(data = filter(POC_model, feature != "Reply from user" & !is.na(std.error) & respondent_type == "Citizen sample"),
                size = 2.75, position = position_nudge(x = 0, y = 0.25), stroke = 1) +
     geom_segment(data = filter(POC_model, feature != "Reply from user" & !is.na(std.error) & respondent_type == "Politician sample"),
                  aes(x = lower, xend = upper), 
                  size = 0.5, position = position_nudge(x = 0, y = -0.25)) +
     geom_point(data = filter(POC_model, feature != "Reply from user" & !is.na(std.error) & respondent_type == "Politician sample"),
                size = 2.75, position = position_nudge(x = 0, y = -0.25),
                fill = "white", stroke = 1) +
     geom_point(data = filter(POC_model, feature != "Reply from user" & is.na(std.error) & respondent_type == "Politician sample"),
                size = 2, position = position_nudge(x = 0, y = 0),
                color = "black", fill = "black") +
     geom_label(data = filter(POC_model, feature == "Gender of politician"),
               aes(x = lower, y = level, label = respondent_type),
               hjust = 1.07, vjust = 0.3, fontface = "bold",
               label.padding = unit(0.1, "lines"), label.size = NA,
               label.r = unit(0.15, "lines"),
               position = position_dodge(width = -1), size = 2.75) +
     scale_color_manual(values = c("#5200FF", "#00bd8e", "#FF3E00", "#A1B811", "blue")) +
     scale_shape_manual(values = c(16, 21)) +
     theme(legend.position = "none",
           strip.text.x = element_text(angle = 0, hjust = 0, size = 8, face = "bold"))
dev.off()
